app.config 配置多项 配置集合 自定义配置(3)
再说说利用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)的更多相关文章
- app.config 配置多项 配置集合 自定义配置(2)
上一篇说了利用app.config自定义节点配置,那是利用工具来实现,其实也一全部编码的方式来实现.举一个栗子.Simpson一家有父亲James,母亲Kate,和三个儿女Jim,Aaron和Luka ...
- app.config 配置多项 配置集合 自定义配置
C#程序的配置文件,使用的最多的是appSettings 下的<add key="Interval" value="30"/>,这种配置单项的很方便 ...
- app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法
一,按照xml文件处理: 配置文件如下图(最后的图片). 自动写入configSections和configSections的实例 1.自动写入configSections Configuration ...
- C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案
MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...
- web.config or app.config 中configSections配置节点
以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...
- 【flask】flask项目配置 app.config
[理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...
- .Net Core 自定义配置源从配置中心读取配置
配置,几乎所有的应用程序都离不开它..Net Framework时代我们使用App.config.Web.config,到了.Net Core的时代我们使用appsettings.json,这些我们再 ...
- C# App.config 详解
读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...
随机推荐
- Sublime Text3使用指南
前言(Prologue) Sublime Text是一款跨平台代码编辑器(Code Editor),从最初的Sublime Text 1.0,到现在的Sublime Text 3.0,Sublime ...
- 聊聊Java语言中的单例
package com.xinke.mybatis.test; public class TestSingleton { private static TestSingleton ts = null; ...
- 【转】python time模块详解
python 的内嵌time模板翻译及说明 一.简介 time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:0 ...
- python random从集合中随机选择元素
1.使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print ...
- Scala基础之注解(annotation
在学习Scala的过程中,总会碰到一些注解: // Predef.scala @inline def implicitly[T](implicit e: T) = e @deprecated(&quo ...
- [转载]AI教师正来势汹汹,教师饭碗堪优
(原文标题:开门,机器人老师来了) 一. 开门,机器人老师到了 国庆几天,河南刚刚上演一幕新科技的大戏: 计算机和人展开了为期四天的人机大战.这一次,对垒的双方不再是李世乭和阿尔法狗,而是教师和人工智 ...
- 框架应用 : Spring MVC - 开发详述
软件开发中的MVC设计模式 软件开发的目标是减小耦合,让模块之前关系清晰. MVC模式在软件开发中经常和ORM模式一起应用,主要作用是将(数据抽象,数据实体传输和前台数据展示)分层,这样前台,后台,数 ...
- LINUX 笔记-top命令
top命令经常用来监控linux的系统状况,比如cpu.内存的使用. top - :: up day, :, users, load average: 0.00, 0.01, 0.00 Tasks: ...
- Git相关操作四
1.克隆远程仓库 git clone remote_location clone_name remote_location为仓库地址,clone_name为要克隆到本地的仓库名称. 2.显示对应克隆地 ...
- MongoDB聚合(count、distinct、group、MapReduce)
1. count:返回集合中文档的数量. db.friend.count() db.friend.count({'age':24}) 增加查询条件会使count查询变慢. 2. distinct:找出 ...