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() 這兩個方法得到不屬于頂層的對象。
轉載于:https://www.cnblogs.com/kevinGao/p/4188739.html
總結
以上是生活随笔為你收集整理的Config程序配置文件操作实践进阶之ConfigurationSectionGroup的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决Vue打包后背景图片路径错误问题
- 下一篇: 第4章:数组与方法