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

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <sectionGroup type="Family.SectionGroups,Family" name="family">
  5. <section name="simpson" type="Family.Simpson.FamilySection,Family" allowDefinition="Everywhere"/>
  6. <section name="wang" type="Family.Wang.WangSection,Family" allowDefinition="Everywhere"/>
  7. </sectionGroup>
  8. </configSections>
  9.  
  10. <family>
  11. <simpson surname="Simpson">
  12. <father firstName="James" lastName="Simpson"/>
  13. <mother firstName="Kate" lastName="Simpson"/>
  14. <children >
  15. <add firstName="Jim" lastName="Simpson"/>
  16. <add firstName="Aaron" lastName="Simpson"/>
  17. <add firstName="Lukas" lastName="Simpson"/>
  18.  
  19. </children>
  20. </simpson>
  21.  
  22. <wang surname="King">
  23. <father firstName="王" lastName="二小"/>
  24. <mother firstName="李" lastName="菲菲"/>
  25. <child firstName="王" lastName="博"/>
  26. </wang>
  27.  
  28. </family>
  29.  
  30. </configuration>

  

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

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

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

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

  1.  
  1.  
  1. class SectionGroups : System.Configuration.ConfigurationSectionGroup
  2. {
  3.  
  4. public Family.Wang.WangSection Wang
  5. {
  6. get { return (Family.Wang.WangSection)base.Sections["wang"]; }
  7. }
  8.  
  9. public Family.Simpson.FamilySection Simpson
  10. {
  11. get { return (Family.Simpson.FamilySection)base.Sections["simpson"]; }
  12. }
  13. }

  测试代码1

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

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

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

  测试代码3,这样也行

  1. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  2.  
  3. ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
  4. foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
  5. {
  6. foreach (ConfigurationSection section in sectionGroup.Sections)//有很多其他默认节点
  7. {
  8. if (section.SectionInformation.Name == "wang")
  9. {
  10. Family.Wang.WangSection wang = section as Family.Wang.WangSection;
  11. Console.WriteLine("father: " + wang.Father.FirstName + " " + wang.Father.LastName);
  12. Console.WriteLine("mother: " + wang.Mother.FirstName + " " + wang.Mother.LastName);
  13. }
  14. if (section.SectionInformation.Name == "simpson")
  15. {
  16. Family.Simpson.FamilySection simpson = section as Family.Simpson.FamilySection;
  17. Console.WriteLine("father: " + simpson.Father.FirstName + " " + simpson.Father.LastName);
  18. Console.WriteLine("mother: " + simpson.Mother.FirstName + " " + simpson.Mother.LastName);
  19. foreach (Family.Simpson. Child child in simpson.Children)
  20. {
  21. Console.WriteLine("child: " + child.FirstName + " " + child.LastName);
  22. }
  23. }
  24. }
  25. }

  

注意:在最上面的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. Sublime Text3使用指南

    前言(Prologue) Sublime Text是一款跨平台代码编辑器(Code Editor),从最初的Sublime Text 1.0,到现在的Sublime Text 3.0,Sublime ...

  2. 聊聊Java语言中的单例

    package com.xinke.mybatis.test; public class TestSingleton { private static TestSingleton ts = null; ...

  3. 【转】python time模块详解

    python 的内嵌time模板翻译及说明  一.简介 time模块提供各种操作时间的函数  说明:一般有两种表示时间的方式:       第一种是时间戳的方式(相对于1970.1.1 00:00:0 ...

  4. python random从集合中随机选择元素

    1.使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print ...

  5. Scala基础之注解(annotation

    在学习Scala的过程中,总会碰到一些注解: // Predef.scala @inline def implicitly[T](implicit e: T) = e @deprecated(&quo ...

  6. [转载]AI教师正来势汹汹,教师饭碗堪优

    (原文标题:开门,机器人老师来了) 一. 开门,机器人老师到了 国庆几天,河南刚刚上演一幕新科技的大戏: 计算机和人展开了为期四天的人机大战.这一次,对垒的双方不再是李世乭和阿尔法狗,而是教师和人工智 ...

  7. 框架应用 : Spring MVC - 开发详述

    软件开发中的MVC设计模式 软件开发的目标是减小耦合,让模块之前关系清晰. MVC模式在软件开发中经常和ORM模式一起应用,主要作用是将(数据抽象,数据实体传输和前台数据展示)分层,这样前台,后台,数 ...

  8. LINUX 笔记-top命令

    top命令经常用来监控linux的系统状况,比如cpu.内存的使用. top - :: up day, :, users, load average: 0.00, 0.01, 0.00 Tasks: ...

  9. Git相关操作四

    1.克隆远程仓库 git clone remote_location clone_name remote_location为仓库地址,clone_name为要克隆到本地的仓库名称. 2.显示对应克隆地 ...

  10. MongoDB聚合(count、distinct、group、MapReduce)

    1. count:返回集合中文档的数量. db.friend.count() db.friend.count({'age':24}) 增加查询条件会使count查询变慢. 2. distinct:找出 ...