再说说利用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个小时.
时间: 2024-11-08 18:17:18