2012-08-15 49 views

回答

0

是的,你可以做到這一點。

<configuration> 
    <configSections> 
     <sectionGroup name="pageAppearanceGroup"> 
      <section 
      name="pageAppearance" 
      type="Samples.AspNet.PageAppearanceSection" 
      allowLocation="true" 
      allowDefinition="Everywhere"/> 
     </sectionGroup> 
    </configSections> 

    <pageAppearanceGroup> 
    <pageAppearance remoteOnly="true"> 
     <font name="TimesNewRoman" size="18"/> 
     <color background="000000" foreground="FFFFFF"/> 
    </pageAppearance> 
    </pageAppearanceGroup> 
</configuration> 

然後訪問的變量

Samples.AspNet.PageAppearanceSection config = (Samples.AspNet.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection(
     "pageAppearanceGroup/pageAppearance"); 

    Response.Write("<h2>Settings in the PageAppearance Section:</h2>"); 
    Response.Write(string.Format("RemoteOnly: {0}<br>", 
     config.RemoteOnly)); 
etc.... 

檢查此鏈接 http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

+0

您的示例使用的屬性,我想要使用的元素 – ManyRootsofAllEvil 2012-08-16 06:34:28

+0

我明白你在說什麼,但是,你不能保存數據在你想要的方法,並通過system.ConfigurationManager訪問它。你可以做得很好,就像我在上面的例子中展示的那樣。爲什麼它有所作爲? – Matt 2012-08-16 06:49:24

+0

唯一的區別是美學,我猜,但你的答案不是真正的答案我的問題。 http://manyrootsofallevilrants.blogspot.co.uk/search/label/Custom%20Configuration – ManyRootsofAllEvil 2012-08-16 14:59:53

0

ConfigurationElement.DeserializeElement默認實現不支持XmlNodeType.Text型或XmlNodeType.CDATA的節點,並拋出一個ConfigurationErrorsException,出現以下錯誤消息:The configuration section cannot contain a CDATA or text element.

因此,要使用元素文本內容存儲信息,請覆蓋ConfigurationElement.DeserializeElement方法。

相關問題