再说说利用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个小时.

app.config 配置多项 配置集合 自定义配置(3)的更多相关文章

  1. app.config 配置多项 配置集合 自定义配置(2)

    上一篇说了利用app.config自定义节点配置,那是利用工具来实现,其实也一全部编码的方式来实现.举一个栗子.Simpson一家有父亲James,母亲Kate,和三个儿女Jim,Aaron和Luka ...

  2. app.config 配置多项 配置集合 自定义配置

    C#程序的配置文件,使用的最多的是appSettings 下的<add key="Interval" value="30"/>,这种配置单项的很方便 ...

  3. app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法

    一,按照xml文件处理: 配置文件如下图(最后的图片). 自动写入configSections和configSections的实例 1.自动写入configSections Configuration ...

  4. C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案

    MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...

  5. C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法

    App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...

  6. web.config or app.config 中configSections配置节点

    以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...

  7. 【flask】flask项目配置 app.config

    [理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...

  8. .Net Core 自定义配置源从配置中心读取配置

    配置,几乎所有的应用程序都离不开它..Net Framework时代我们使用App.config.Web.config,到了.Net Core的时代我们使用appsettings.json,这些我们再 ...

  9. C# App.config 详解

      读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...

随机推荐

  1. 项目管理软件之争,禅道和JIRA大对比

    本文摘要: 一. 产品介绍 二. 界面设计 1. 界面颜色设计 2. 布局结构 三. 功能区别 四. 价格对比 五. 后期服务 六. 优缺点 七. 总结 说到项目管理软件,不得不提的是禅道和JIRA. ...

  2. VBA 中窗体模式切换,一次设计2种表现

    Sub ModelChange() Then DoCmd.RunCommand acCmdSubformFormView ''''就这句 Me.Form.AllowEdits = True ' Mod ...

  3. C# 中函数内定义函数的委托方法

    //定义委托方法Action(无返回值)Func(有返回值) //无返回值委托 Action<string> SetKeyAndValue = delegate(string key) { ...

  4. 从 JavaScript 到 TypeScript 系列

    随着应用的庞大,项目中 JavaScript 的代码也会越来越臃肿,这时候许多 JavaScript 的语言弊端就会愈发明显,而 TypeScript 的出现,就是着力于解决 JavaScript 语 ...

  5. try catch finally 中包含return的几种情况,及返回结果

    当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况:在try和catch中有return,finally中没有return,且finally中没有对try或catch中要 retu ...

  6. PPT排版细节,写给大家看的设计书,完美总结

    原创作者:陈玓玏 相信每一位小宝贝在工作中都会被老板用PPT虐无数遍,虐到自己怀疑人生.奈何在网上随手一搜,出现的各类招聘要求都躲不开"熟练掌握PPT制作",尤其是各类科技公司.咨 ...

  7. VUE长按事件

    PS:在开发中常常会有长按事件的需求,这里我简单的介绍几种长按事件的需求 需求一:长按数字累加或者累减 HTML: <div class="mui-numbox" data- ...

  8. 张高兴的 Windows 10 IoT 开发笔记:DHT11 温湿度传感器

    GitHub : https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/DHT11Demo

  9. 怎样在Win10下安装ubuntu双系统

    Win10系统下安装ubuntu系统 安装前准备: 概念 在动手之前,一定要先了解双系统.系统引导.分区这3个概念,这样才能理解安装步骤,应对安装过程中的意外情况. 双系统 双系统就是开机之后,会有一 ...

  10. GoldenGate 复制进程报错"OGG-01296 Error mapping",丢弃文件报错“Mapping problem with delete record (target format)”,且实际条目存在

    故障描述: (1).复制进程 Abended,通过view report语句查看可发现类似如下的报错: 2017-10-23 15:01:43 ERROR OGG-01296 Error mappin ...