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