读取具有相同嵌套元素的自定义配置部分
本文关键字:自定义 配置部 元素 嵌套 读取 | 更新日期: 2023-09-27 18:26:47
想要创建自定义配置部分,在那里我需要读取相同的嵌套元素。
如果您在下面的示例XML中看到,设置元素可以重复到嵌套级别。
请告诉我,如果嵌套元素名称相同,我将如何定义实体来读取这种嵌套结构?
注意:请确保I需要读取同一嵌套元素的集合,该集合可以重复多次。例如"设置"
<customConfigSection>
<setting source="">
<setting from="web1" to="web2" source="" />
<setting from="web3" to="web4" source="" />
<setting from="web5" to="web5" source="" />
<setting from="web7" to="web6" source="" />
<setting from="domain2" to="" source="">
<setting from="web1" to="web2" source="" />
<setting from="web1" to="web2" source="" />
<setting from="web1" to="web2" source="" />
</setting>
<setting>
<customConfigSection>
您可以使用ConfigurationSection:来完成此操作
public class SomeSettings : ConfigurationSection
{
private SomeSettings() { }
[ConfigurationProperty("FillColor", DefaultValue="Cyan")]
public System.Drawing.Color FillColor
{
get { return (System.Drawing.Color)this["FillColor"]; }
set { this["FillColor"] = value; }
}
[ConfigurationProperty("TextSize", DefaultValue="8.5")]
public float TextSize
{
get { return (float)this["TextSize"]; }
set { this["TextSize"] = value; }
}
[ConfigurationProperty("FillOpacity", DefaultValue="40")]
public byte FillOpacity
{
get { return (byte)this["FillOpacity"]; }
set { this["FillOpacity"] = value; }
}
}
web.config:
<configuration>
<configSections>
<section name="SomeSettings" type="MyApp.SomeSettings, SomeSettings" />
</configSections>
<SomeSettings FillColor="LightBlue" TextSize="9.5" FillOpacity="50" />
</configuration>
这是代码项目中的完整文章