4

我有这样的一个web.config:读取web.config部分,列出

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

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

感谢,

+1

没有...在谷歌搜索,但没有找到任何答案(或没有使用正确的关键字) –

+1

Google [“custom con figuration section“](http://www.google.com/search?q=custom+configuration+section) – abatishchev

回答

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

或者那不工作,或者片断丢失或者我很蠢,但这不起作用 –

+0

@ Kris-I:我添加了更多的代码,但是您肯定必须阅读更多关于该主题的内容,尝试更多自己,然后发布一些结果或错误(如果有的话) – abatishchev

+0

@ Kris-I:在此处查看。 MyElementCollection已经包含元素,现在添加属性'MyAttribute'。 – abatishchev

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); 
} 
+0

MySection在当前上下文中不存在... –

+0

@ Kris-I:当然它还不存在。您需要先定义它。 – abatishchev