今天又进一步对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. java.io.ioexception failed to mkdirs jenkins xcode || jenkins 无法创建新文件

    =========================================================== FATAL: Failed to mkdirs: /Users/chenqing ...

  2. Linux下安装Supervisor的多种方法

    一.安装 1.方法一: pip install  supervisor #!/bin/bash wget http://pypi.python.org/packages/source/s/setupt ...

  3. Ubuntu 12.04.1 mysql从5.5升级到5.6

    Ubuntu 12.04.1 mysql从5.5升级到5.6 1 2 3 4 5 apt-cache search mysql-server sudo  apt-add-repository ppa: ...

  4. JAVA开发总结

    1.Float取整 long warning; float fwarning = 0f; fwarning = total*0.34/100; warning = new Float(fwarning ...

  5. Win10 PLSQL 登录后,提示数据库字符集(AL32UTF8)和客户端字符集(ZHS16GBK)不一致

    plsql 登录后提示: Database character set (AL32UTF8) and Client character set (ZHS16GBK) are different.Cha ...

  6. Mule 入门之:环境搭建

    Mule 入门之:环境搭建 JDK1.5或以上版本Eclipse3.3以上 下载与安装:目前最新版本为2.2.1 下载,下载后得到一名为mule-standalone-2.2.1.zip的压缩文件,解 ...

  7. 获取ul下li标签里点击的是哪一个li并获取li里a标签的值

    $("ul li").click(function(){ //获取当天月份 ).text(); ,) alert(month); }); <!doctype html> ...

  8. JNI 引用问题梳理(转)

    局部引用: JNI 函数内部创建的 jobject 对象及其子类( jclass . jstring . jarray 等) 对象都是局部引用,它们在 JNI 函数返回后无效: 一般情况下,我们应该依 ...

  9. Axis 发布、调用WebService(转)

    一.JWS方法发布WebService 1.在官方网站下载axis的工程(这个等下就有用的)和源码.jar包等,下载地址是: http://www.apache.org/dyn/closer.cgi/ ...

  10. Selenium+TestNG+Jenkins 框架图形化UML表示