2009-09-01 72 views
1

除非我做錯了什麼,順便我應該使用的ConfigurationSection,和的ConfigurationElement ConfigurationElementCollection中,要求我格式化我的配置部分,像這樣:web.config中的自定義配置節和不必要的冗長

<serviceAuthorization> 
    <credentials> 
     <login username="system" password="password" mode="include"> 
      <services> 
       <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" /> 
       <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" /> 
      </services> 
     </login> 
     <login username="test" password="pass" mode="exclude" /> 
    </credentials> 
</serviceAuthorization> 

我如果我在格式中多說一點,會更喜歡。我想格式化我的部分是這樣的:

<serviceAuthorization> 
    <login username="system" password="password" mode="include"> 
     <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" /> 
     <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" /> 
    </login> 
    <login username="test" password="pass" mode="exclude" /> 
</serviceAuthorization> 

有沒有一種方法,我可以得到配置節的XML,只是讀它自己?

回答

1

您可以實現System.Configuration.IConfigurationSectionHandler並配置它:

<section name="serviceAuthorization" type="[your-type]"/> 

然後你得到你的整個sectionXmlNode並且可以分析您的自定義模式。

編輯:這已棄用。這裏有一個new way來做到這一點。

+0

據我瞭解,IConfigurationSectionHandler已被棄用 – 2009-09-01 08:08:03

+0

你是絕對正確的。我將它添加到我的答案中。 – 2009-09-01 08:52:01

0

嗯,你可以做,例如:

string docName=System.Web.HttpContext.Current.Server.MapPath("Web.config"); 
XmlDocument configDoc = new XmlDocument(); 
configDoc.Load(docName); 

,然後從configDoc工作。

+1

我知道那條路線;我只是希望稍微不太粗魯。 – 2009-09-01 08:08:34