再说说利用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. 在Pycharm中使用jupyter笔记本

    在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...

  2. Sql语句varchar或nvarchar字段条件前加N的性能差异

    Sql语句varchar或nvarchar字段条件前加N的话是对这个字段进行Unicode编码, 这样做的目的是避免在这种字段中存入俄文.韩文.日文的情况下有可能会出现乱码. 但这样做也会有性能问题, ...

  3. 实用的Jquery选项卡TAB

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、属性配置、多环境配置

    1 InteliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到官 ...

  5. jsp与servlet联合处理

    通过model1 或者说 model2 我们知道的mvc 的分层思想, 在model1 中jsp 通负责显示,有负责控制逻辑结构, 那么怎么做呢,看下面的请求过程. <?xml version= ...

  6. .NET接入接口/请求服务器

    之前只调用过自己写的接口,这个是调用外部接口 一.创建方法链接接口 , string method = "Get", string token = null) { if (stri ...

  7. 『实践』Yalmip建模+Cplex类求解

    Yalmip建模+Cplex类求解 一.缘由 Yalmip只能设置部分Cplex的参数,所以需要调用Cplex类.而且optimize是Yalmip提供的常用函数,但此函数的返回结果参数有限. 图1 ...

  8. 快速部署MongoDB

    MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.本文安装的版本为3.0,其他版本可对照. 设置mongodb repo vi /e ...

  9. Windows7 64位环境下Python-igraph环境配置

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 其实配置很简单,但是网上的几个都不咋靠谱.而且我本身对这个软件挺感兴趣.igraph是一个网络 ...

  10. 对于Swift的Enum,文档上没有说的

    今天无意发现一个东西, 但是在文档上看了很多遍都没找到, 但是亲测是可行的, 那到底是什么呢? 以前我们定义枚举 会这样: enum Hello { case Item( String, Int) c ...