2009-09-01 76 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