今天又进一步对System.Configuration下的ConfigurationSectionGroup类及相关的类与方法进行了研究。发现要构建多层次嵌套的XML标签 则必须用到ConfigurationSectionGroup类

我们看一下下面这个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="appGroupC" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
</sectionGroup>
<sectionGroup name="appGroupD" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
</sectionGroup>
<sectionGroup name="appGroupE" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
</sectionGroup>
<sectionGroup name="MyGroup" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
<section name="Nameissection1" type="Study_System.Configuration.AppSectionA, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<sectionGroup name="GroupAB" type="Study_System.Configuration.AppGroup, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
<section name="NameissectionA" type="Study_System.Configuration.AppSectionB, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<section name="NameissectionB" type="Study_System.Configuration.AppSectionB, Study_System.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</sectionGroup>
</sectionGroup>
</configSections>
<connectionStrings>
<add name="连接的名称,就类同于Key" connectionString="具体的连接字符串" providerName="与连接字符串一起使用的提供程序的名称。这个有时可以没有" />
</connectionStrings>
<appSettings>
<add key="huangbo" value="1234567890" />
</appSettings>
<MyGroup>
<Nameissection1 KeyName="" KeyValue="">
<AppElement KeyName="this is key" KeyValue="this is value" KeyValue2="this is value2" />
</Nameissection1>
<GroupAB>
<NameissectionA KeyName="hahahaha" KeyValue="1234567">
<ElementCollection KeyName="" />
</NameissectionA>
<NameissectionB KeyName="this is key name" KeyValue="this is key value">
<ElementCollection KeyName="">
<add KeyName="e1 key" KeyValue="e1 value" KeyValue2="e1 value2" />
<add KeyName="e2 key" KeyValue="e2 value" KeyValue2="e2 value2" />
<add KeyName="e3 key" KeyValue="e3 value" KeyValue2="e3 value2" />
</ElementCollection>
</NameissectionB>
</GroupAB>
</MyGroup>
</configuration>

可以发现只要是ConfigurationSectionGroup一样会出现在区域的声明部分<configSections>的标签内。以显示出所有Section的层次。

然后在下面则按上这个层次的所有Section内容。

使用ConfigurationSectionGroup对象非常方便。

一、创建有多个层次嵌套的XML

1、直接使用ConfigurationSectionGroup或写一个类继承ConfigurationSectionGroup。在这个类中无法像Section和元素一样添加属性。

2、将Section添加到ConfigurationSectionGroup中。如果是多个层次嵌套的,还可以将ConfigurationSectionGroup对象添加到ConfigurationSectionGroup对象中。这就像数组中还有数组一样。

3、将最顶层的ConfigurationSectionGroup对象添加到Configuration中。

二、如果读取有ConfigurationSectionGroup对象的内容

1、在读取有ConfigurationSectionGroup对象的时候我们可以用Configuration.GetSectionGroup("GroupName")来得到它,记得要将得到的Group转换为相对应的继承于ConfigurationSectionGroup的那个类。

2、得到相应的继承于ConfigurationSectionGroup类的对象后,可以用ConfigurationSectionGroup.Sections.Count得到这个Group下Section的数量,用ConfigurationSectionGroup.Sections[0].SectionInformation.Name 得到Section的名字

例如:得到示例XML中MyGroup对象的Name和AppSectionB对象的Name的代码为:

((AppGroup)cfg.GetSectionGroup("MyGroup")).Name;
((AppSectionB)cfg.GetSectionGroup("MyGroup").SectionGroups[0].Sections[1]).KeyName

三、关于在一个Section下有多个元素

如果有多个元素,必须用一个用户类来继承ConfigurationElementCollection。不要试图在继承于Section类的自定义类中用AppElement的数组方式。因为ConfigurationManager无法从XML中读取到不被ConfigurationElementCollection对象包含的元素数组。

一个很直观的例子就是.NET预定义的二个对象:Configuration.AppSettings.Settings 和 Configuration..ConnectionStrings.ConnectionStrings 其中我们看到AppSettings和ConnectionStrings是继承于Section类的对象,Settings 和 ConnectionStrings  则是继承于ConfigurationElementCollection的对象。所以我们在设置XML的结构中应该参数它这个的形式,且目前来看也没有其它办法可以读取到不被ConfigurationElementCollection包含的元素。

四、关于Configuration.SectionGroups.Count 和 Configuration.Sections.Count

如果你的Section对象是直接加到Configuration中去的,那么这个时候你可能要杯具了。因为由于Configuration中存在了很多.NET预定义好的Section和Group所以你用Configuration.SectionGroups.Count将得到10。Configuration.Sections.Count将得到22。这个时候你千万不要奇怪和郁闷,可以使用Configuration.GetSectionGroup(GroupName)先得到顶层的你定义的那个Group,之后在这个Group下面的SectionGroups.Count
和 Sections.Count 将是正常的。

换句话说就是你无法直接从Configuration的GetSectionGroup() 及 GetSection() 这两个方法得到不属于顶层的对象。

Config程序配置文件操作实践进阶之ConfigurationSectionGroup的更多相关文章

  1. Config程序配置文件(configSections)操作实践及代码详注

    所有与配置文件相关的类:(粗体为一般情况下使用到的类,其它类功能可能在很复杂的情况下才使用到.) 1.ConfigurationManager,这个提供用于打开客户端应用程序集的Configurati ...

  2. .NET程序配置文件操作(ini,cfg,config)

    在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...

  3. C# 应用程序配置文件操作

    应用程序配置文件,对于asp.net是 web.config对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本质 ...

  4. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作

    原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...

  5. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  6. C# 应用程序配置文件App.Config和web.config

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  7. C#应用程序配置文件.config介绍

    我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...

  8. .NET 多个程序配置文件合并到主app.config

    .NET 多个程序配置文件合并到主app.config

  9. 配置文件操作(ini、cfg、xml、config等格式)

    配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...

随机推荐

  1. Refuses to install for WTP10

    Refuses to install for WTP10   description http://imgur.com/a/QN4iM I am a WTP10 user. I’m running b ...

  2. C# 动态解析表达式

    需求 我们很难捉摸用户的思维,即使使用非常正式的文档规范某些数据的定义.结果的标准等,也不能抵挡住用户不断变化的需求,但他们有个万变不离的东西——你做这个东西要是万能的,即输入参数类型.个数等发生改变 ...

  3. android中使用SharedPreferences存储数据

    使用SharedPreferences存储数据还是比较简单的 1.添加或修改数据(没有数据就添加,有数据就是修改): SharedPreferences.Editor editor = getShar ...

  4. jQuery 发送验证码倒计时按钮 复制代码

    <!DOCTYPE html> <html> <head> <title></title> <script src="../ ...

  5. jQuery函数的等价原生函数代码示例

    选择器 jQuery的核心之一就是能非常方便的取到DOM元素.我们只需输入CSS选择字符串,便可以得到匹配的元素.但在大多数情况下,我们可以用简单的原生代码达到同样的效果. .代码如下: //---- ...

  6. javascript链式语法

    因为 jQuery 库的缘故,链式语法在前端界变得非常流行.实际上这是一种非常容易实现的模式.基本上,你只需要让每个函数返回 'this',这样其他函数就可以立即被调用.看看下面的例子. var bi ...

  7. WinForm 之 程序退出

    一.关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.ExitThread(); System.En ...

  8. C#的ThreadStart 和 Thread

    多线程,new Thread(t1);和new Thread(new ThreadStart(t1));有什么区别 没有区别. 前者,是c#的语法.也就是说是编译器帮你改写为第二种形式. 因此你要搞清 ...

  9. 玩转Bootstrap

    一:bootstrap基本模版 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  10. 〖Linux〗Linux的smb地址转换Windows格式(两者互转)

    因为个人常用办公PC是Linux,打开文件共享什么的是 smb:// 的,而不是Windows的 \\ 需要复制文件路径给别人的时候,发smb://给一个使用Windows办公的用户不算很得体的方法 ...