将web.config部分读取到List

时间:2011-11-03 07:01:29

标签: c# .net configuration configurationsection custom-configuration

我在web.config中有这个:

<MySection>
    <Setting1 Value="10" />
    <Setting2 Value="20" />
    <Setting3 Value="30" />
    <Setting4 Value="40" />
</MySection>

我想阅读所有部分“MySection”并获取所有值List<string>(例如:“10”,“20”,“30”)

谢谢,

2 个答案:

答案 0 :(得分:5)

首先,我建议使用Unity Configuration

代码:

public class MySection : ConfigurationSection
{
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();

    private static ConfigurationProperty propElements = new ConfigurationProperty("elements", typeof(MyElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);

    static BotSection()
    {
        properties.Add(propElements);
    }

    [ConfigurationProperty("elements", DefaultValue = null, IsRequired = true)]
    [ConfigurationCollection(typeof(MyElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
    public MyElementCollection Elements
    {
        get
        {
            return (MyElementCollection)this[propElements];
        }
        set
        {
            this[propElements] = value;
        }
    }
}

public class MyElementCollection : ConfigurationElementCollection, 
                                   IEnumerable<ConfigurationElement> // most important difference with default solution
{
    public void Add(MyElement element)
    {
        base.BaseAdd(element);
    }

    public void Clear()
    {
        base.BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new MyElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((MyElement)element).Id;
    }

    IEnumerator<MyElement> IEnumerable<MyElement>.GetEnumerator()
    {
        return this.OfType<MyElement>().GetEnumerator();
    }
}

public class MyElement : ConfigurationElement
{
    protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();

    private static ConfigurationProperty propValue= new ConfigurationProperty("value", typeof(int), -1, ConfigurationPropertyOptions.IsRequired);

    public int Value
    {
        get
        {
            return (int)this[propValue];
        }
        set
        {
            this[propValue] = value;
        }
    }
}

配置:

<configuration>
    <configSections>
        <section name="MySection" type="MySection, MyAssembly"/>
    </configSections>
    <MySection>
        <elements>
            <clear />
            <add value="10" />
            <remove value="10" />
            <add value="20" />
            <add value="30" />
        </elements>
    </MySection>
</configuration>

答案 1 :(得分:2)

我建议你看一下CodePlex上优秀的开源Configuration Section Designer项目。它允许您使用Visual Studio中托管的设计器创建自定义配置节。

例如,自定义配置部分设计如下:

Simple Custom Section 将产生如下配置文件:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="MySection" type="MyNamespace.MySection, MyAssembly"/>
  </configSections>
  <MySection xmlns="urn:MyNamespace">
    <MySetting Name="Test1" Value="One" />
    <MySetting Name="Test2" Value="Two" />
  </MySection>
</configuration>

可以像这样以编程方式使用:

foreach (MySetting setting in MySection.Instance.Items)
{
    Console.WriteLine("{0}: {1}", setting.Name, setting.Value);
}