【问题标题】:Read custom configuration section which is having same nested element阅读具有相同嵌套元素的自定义配置部分
【发布时间】:2014-10-16 09:11:04
【问题描述】:

想要创建自定义配置部分,我需要在其中读取相同的嵌套元素。

如果您在下面的示例 XML 中看到,setting 元素可以重复到嵌套级别。

请任何人告诉我,我将如何定义实体来读取这种嵌套元素名称也相同的嵌套结构?

注意:请确保我需要读取可以重复多次的同一个嵌套元素的集合。例如。 '设置'

<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>

【问题讨论】:

    标签: c# .net xml configuration web-config


    【解决方案1】:

    您可以使用 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>
    

    Here是代码项目中的完整文章

    【讨论】:

    • 谢谢,但是这个问题是关于读取同一个嵌套元素集合的。
    • 我认为有一个例子也可以描述这一点。
    • 不,这里提到的例子不适合我的要求。
    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多