實作ConfigurationElementCollection

如何實作ConfigurationElementCollection

在上一篇文章,介紹如何實作ConfigurationElement來存取某一個設定檔內多個欄位值,

而本篇則介紹如何透過ConfigurationElementCollection來存取以下的設定集合:

<HackSettings>

<add OrderName="Kim"/>

<add OrderName="Jia"/>

</HackSettings>

 

1.實作ConfigurationElement

ConfigurationElement

 

 

 

2.實作ConfigurationElementCollection

ConfigurationElementCollection

 

 

3.實作ConfigurationSection

 

ConfigurationSection

 

 

4.組態設定

<configSections>

<section name="HackConfigSection" type="Kim.Configuration.HackConfigSection,Kim.Configuration"/>

</configSections>

<HackConfigSection>

<HackSettings>

<add OrderName="Kim"/>

<add OrderName="Jia"/>

</HackSettings>

</HackConfigSection>

 

Ps:紅字為命名空間+類別名稱

     藍字為組件名稱

 

5.程式測試

  KimSection kimObj = System.Configuration.ConfigurationManager.GetSection("Kim") as KimSection;

 

            Response.Write("Username:" + kimObj.KimReportServer.Username + "<br>");

            Response.Write("Password:" + kimObj.KimReportServer.Password + "<br>");

            Response.Write("Url:" + kimObj.KimReportServer.Url + "<br>");

 

參考文件

http://msdn.microsoft.com/zh-tw/library/system.configuration.configurationsection.aspx

 

 

完整範例下載
 ConfigurationSample.rar