再说说利用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. Oracle添加含有脏数据的约束

    需求: 一个表的唯一约束被禁用期间,有脏数据进来,当启用约束时失败. 环境: -bash-4.1$ uname -a Linux dbtest1 2.6.32-279.el6.x86_64 #1 SM ...

  2. Request.QueryString("id")与Request("id")区别

    Request从几个集合取数据是有顺序的,从前到后的顺序依次是 QueryString,Form,最后是ServerVariables.Request对象按照这样的顺序依次搜索这几个集合中的变量,如果 ...

  3. Cmder 软件中修改λ符号方法

    以前的版本 网上都有,我就不介绍了,  只介绍现在的 1. 打开Cmder软件安装位置 2. 打开vendor文件夹 profile.ps1文件 3. 找到第77行  Write-Host " ...

  4. localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车

    localStorage.setItem("key","value");//存储变量名为key,值为value的变量 localStorage.key = &q ...

  5. Linux文件类型介绍

    文件类型介绍: Linux系统不同于Windows系统,两者文件类型和文件扩展名也有很大的差异.Linux中的文件类型和Linux文件的文件扩展名所代表的意义和Windows系统完全不同.用户一般通过 ...

  6. mac idea sbt工程打jar包

    1.首先保证sbt已下载,否则下载homebrew:在命令行输入/usr/bin/ruby XXX ->下载完成后在终端输入brew install sbt ->安装完毕后可以打jar包 ...

  7. Django实现用户密码重置

    使用Django内置的认证视图实现简单的通过邮箱重置密码的功能版本:django 1.11 在django.contrib.auth.views中提供了四个类视图用于密码重置 class Passwo ...

  8. Sqoop1.99.7将MySQL数据导入到HDFS中

    准备 本示例将实现从MySQL数据库中将数据导入到HDFS中 参考文档: http://sqoop.apache.org/docs/1.99.7/user/Sqoop5MinutesDemo.html ...

  9. python 目录文件

    每天写一点,总有一天我这条咸鱼能变得更咸 python 中对文件及目录的操作基本依赖与os,shutil模块,其中以os模块为主,最主要的几个方法实例如下: 1.判断文件/目录是否存在(os.path ...

  10. win10 uwp 应用转后台清理内存

    我在写小说阅读器,把每个打开的文件的内容读到内存,因为小说都很小,所以放在内存不怕太大,但是我如果打开了一本小说,再打开一本,我不会把先打开的小说的内容清除掉,在内存.所以一旦我打开多小说的时候,内存 ...