Config程序配置文件操作实践进阶之ConfigurationSectionGroup
今天又进一步对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的更多相关文章
- Config程序配置文件(configSections)操作实践及代码详注
所有与配置文件相关的类:(粗体为一般情况下使用到的类,其它类功能可能在很复杂的情况下才使用到.) 1.ConfigurationManager,这个提供用于打开客户端应用程序集的Configurati ...
- .NET程序配置文件操作(ini,cfg,config)
在程序开发过程中,我们一般会用到配置文件来设定一些参数.常见的配置文件格式为 ini, xml, config等. INI .ini文件,通常为初始化文件,是用来存储程序配置信息的文本文件. [Log ...
- C# 应用程序配置文件操作
应用程序配置文件,对于asp.net是 web.config对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本质 ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
原文 http://www.cnblogs.com/codealone/archive/2013/09/22/3332607.html 应用程序配置文件,对于asp.net是 web.config,对 ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- C# 应用程序配置文件App.Config和web.config
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- C#应用程序配置文件.config介绍
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- .NET 多个程序配置文件合并到主app.config
.NET 多个程序配置文件合并到主app.config
- 配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...
随机推荐
- [Spring Boot] Singleton and Prototype
When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they ...
- GPUImage简单滤镜使用(一)
今天来学习一下一个简单滤镜使用的流程,通过调节亮度滤镜来了解.先将GPUImage库导入到项目中,引入头文件"GPUImage.h" 一.创建亮度滤镜对象 GPUImag ...
- Jenkins Xcode 证书设置错误 Code Sign error: No matching codesigning identity found: No codesigning identities
Jenkins 集成Xcode 项目的时候在证书上遇到了问题.实际上如果在本地的话.只要Xcode工程里选择了项目就不需要重新设置证书了.jenkins会自动找到这个证书,只要在build setti ...
- 微信小程序 - 支持html空格(提示)
仅限于text标签,decode参数:官方api.
- 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- SQL Server中order by的使用,我们来填坑
看似很简单是不是? 单列排序,没有任何问题 select * from tableA where age>1 order by age /*后面可以跟上ASC.DESC,默认是ASC升序排列*/ ...
- Storm本地模式异常
来自:http://isuifengfei.iteye.com/blog/1998265 问题1 java.net.SocketException: Address family not supp ...
- HTTPS与HTTP协议区别
1.主要区别 (1)超文本传输协议HTTP,HTTP下加入SSL层(位于传输层)的安全协议. (2)HTTP端口为80,HTTPS端口是443. 2.HTTPS通信过程 https 通信传输过程:
- HDU1211 RSA
RSA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- JVM的结构
参考:http://blog.csdn.net/tonytfjing/article/details/44278233 JVM的结构 一般认为,JVM分为四大部分: 1.类加载器(ClassL ...