在我见过的所有自定义配置示例中,似乎没有人使用元素来存储数据,例如
<data name="1">
<server>aServer</server>
<ip>anipaddress</ip>
</data>
这真的可能吗?
我知道我可以使用这样的属性:
<data name="1" server="aServer" ip="anipaddress"/>
TIA
在我见过的所有自定义配置示例中,似乎没有人使用元素来存储数据,例如
<data name="1">
<server>aServer</server>
<ip>anipaddress</ip>
</data>
这真的可能吗?
我知道我可以使用这样的属性:
<data name="1" server="aServer" ip="anipaddress"/>
TIA
ConfigurationElement.DeserializeElement的默认实现不支持XmlNodeType.Text
or类型的节点,XmlNodeType.CDATA
并抛出ConfigurationErrorsException
带有以下错误消息的 a:The configuration section cannot contain a CDATA or text element.
因此,要使用元素文本内容存储信息,请覆盖该ConfigurationElement.DeserializeElement
方法。
是的,你可以这么做。
<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....