首页 技术 正文
技术 2022年11月9日
0 收藏 687 点赞 3,731 浏览 3020 个字

再说说利用app.config配置多个自定义的方法.
先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup type="Family.SectionGroups,Family" name="family">
<section name="simpson" type="Family.Simpson.FamilySection,Family" allowDefinition="Everywhere"/>
<section name="wang" type="Family.Wang.WangSection,Family" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections> <family>
<simpson surname="Simpson">
<father firstName="James" lastName="Simpson"/>
<mother firstName="Kate" lastName="Simpson"/>
<children >
<add firstName="Jim" lastName="Simpson"/>
<add firstName="Aaron" lastName="Simpson"/>
<add firstName="Lukas" lastName="Simpson"/> </children>
</simpson> <wang surname="King">
<father firstName="王" lastName="二小"/>
<mother firstName="李" lastName="菲菲"/>
<child firstName="王" lastName="博"/>
</wang> </family></configuration>

  

各个Section和ConfigurationElementCollection的实现参见上一篇文章.此处不贴.
注意老王美国家庭区别是:老王家由于只有child,所以没有children,只有一个child属性.

增加一个SectionGroups类,继承自System.Configuration.ConfigurationSectionGroup.

这个SectionGroups类和配置文件中,下面这句话

<sectionGroup type=”Family.SectionGroups,Family” name=”family”>
这句话是对应的,
“Family.SectionGroups”命名空间+类名,Family为程序集;name=”fanmily”为配置节点的名称.



class SectionGroups : System.Configuration.ConfigurationSectionGroup
{ public Family.Wang.WangSection Wang
{
get { return (Family.Wang.WangSection)base.Sections["wang"]; }
} public Family.Simpson.FamilySection Simpson
{
get { return (Family.Simpson.FamilySection)base.Sections["simpson"]; }
}
}

  测试代码1

SectionGroups sample = (SectionGroups)System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None).SectionGroups["family"];
Family.Wang.WangSection w = sample.Wang;
Father f= w.Father;
Mother m = w.Mother;
Child c = w.Child; Family.Simpson.FamilySection s = sample.Simpson;
// do to for s.Father; s.Mother;s.Children

  测试代码2,也可以这样使用:

Family.Wang.WangSection w = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).GetSection("family/wang") as Family.Wang.WangSection;

  测试代码3,这样也行

 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
foreach (ConfigurationSection section in sectionGroup.Sections)//有很多其他默认节点
{
if (section.SectionInformation.Name == "wang")
{
Family.Wang.WangSection wang = section as Family.Wang.WangSection;
Console.WriteLine("father: " + wang.Father.FirstName + " " + wang.Father.LastName);
Console.WriteLine("mother: " + wang.Mother.FirstName + " " + wang.Mother.LastName);
}
if (section.SectionInformation.Name == "simpson")
{
Family.Simpson.FamilySection simpson = section as Family.Simpson.FamilySection;
Console.WriteLine("father: " + simpson.Father.FirstName + " " + simpson.Father.LastName);
Console.WriteLine("mother: " + simpson.Mother.FirstName + " " + simpson.Mother.LastName);
foreach (Family.Simpson. Child child in simpson.Children)
{
Console.WriteLine("child: " + child.FirstName + " " + child.LastName);
}
}
}
}

  

注意:在最上面的app.config文件中,我开始忘记了<family></family>标签,导致读出来的数据始终为空的,白白浪费我2个小时.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297