在我看到的所有定製配置示例中,似乎沒有人使用元素來存儲數據,例如,.NET中的自定義配置 - 使用元素而不是屬性
<data name="1">
<server>aServer</server>
<ip>anipaddress</ip>
</data>
這實際上是可能的嗎?
我知道我可以使用屬性如下:
<data name="1" server="aServer" ip="anipaddress"/>
TIA
在我看到的所有定製配置示例中,似乎沒有人使用元素來存儲數據,例如,.NET中的自定義配置 - 使用元素而不是屬性
<data name="1">
<server>aServer</server>
<ip>anipaddress</ip>
</data>
這實際上是可能的嗎?
我知道我可以使用屬性如下:
<data name="1" server="aServer" ip="anipaddress"/>
TIA
是的,你可以做到這一點。
<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....
的ConfigurationElement.DeserializeElement默認實現不支持XmlNodeType.Text
型或XmlNodeType.CDATA
的節點,並拋出一個ConfigurationErrorsException
,出現以下錯誤消息:The configuration section cannot contain a CDATA or text element.
因此,要使用元素文本內容存儲信息,請覆蓋ConfigurationElement.DeserializeElement
方法。
您的示例使用的屬性,我想要使用的元素 – ManyRootsofAllEvil 2012-08-16 06:34:28
我明白你在說什麼,但是,你不能保存數據在你想要的方法,並通過system.ConfigurationManager訪問它。你可以做得很好,就像我在上面的例子中展示的那樣。爲什麼它有所作爲? – Matt 2012-08-16 06:49:24
唯一的區別是美學,我猜,但你的答案不是真正的答案我的問題。 http://manyrootsofallevilrants.blogspot.co.uk/search/label/Custom%20Configuration – ManyRootsofAllEvil 2012-08-16 14:59:53