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 = ...
随机推荐
- vue脚手架使用swiper /引入js文件/引入css文件
1.安装vue-cli 参考地址:https://github.com/vuejs/vue-cli 如果不使用严格语法需要在后三项打no:(加了挺头疼的,老是报错,但是对自己的代码规范性也是有很大的帮 ...
- 聊聊Java语言中的单例
package com.xinke.mybatis.test; public class TestSingleton { private static TestSingleton ts = null; ...
- Jquery EasyUI Base基础
<pre><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...
- MVC使用jQuery从视图向控制器传递Model,数据验证,MVC HTML辅助方法小结
//MVC HTML辅助类常用方法记录 (1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(model ...
- 在SQL中用正则表达式替换html标签(2)
由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都替换掉.当然我可以通过写一个程序去修改,那毕竟有点麻烦.直接在查询分析器中执行,但是MS SQL Server并没有 ...
- Bootstrap文本排版基础--Bootsrap
1.排版前的基础 (1)移动设备优先 <meta name="viewport" content="width=device-width, initial-scal ...
- akka tips
1.actorSelection,当涉及actor远程通信时,可以使用actorSelection. context.actorSelection("akka.tcp://app@other ...
- Sqoop1.99.7将MySQL数据导入到HDFS中
准备 本示例将实现从MySQL数据库中将数据导入到HDFS中 参考文档: http://sqoop.apache.org/docs/1.99.7/user/Sqoop5MinutesDemo.html ...
- 【框架学习与探究之AOP--Castle DynamicProxy】
声明 本文欢迎转载,原始地址:http://www.cnblogs.com/DjlNet/p/7603654.html 前言 先说一点废话,在此之前博主也在早期就接触了或者看了些许AOP相关的文章,然 ...
- win10 uwp 获取指定的文件 AQS
很多时候不需要获取整个文件夹的文件,是需要获取文件夹里指定的文件. 那么 UWP 如何对文件夹里的文件进行过滤,只拿出自己需要的文件? 本文:如何使用通配符或文件匹配方式在uwp获取文件夹中指定的文件 ...