Прочитайте раздел web.config в список
У меня есть это в web.config:
<MySection>
<Setting1 Value="10" />
<Setting2 Value="20" />
<Setting3 Value="30" />
<Setting4 Value="40" />
</MySection>
Я хотел бы прочитать весь раздел "MySection" и получить все значения для List<string>
(например: "10","20","30")
Спасибо,
3 ответа
Прежде всего, я рекомендую использовать для использования 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;
}
}
}
Config:
<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>
Я бы посоветовал вам взглянуть на отличный проект конструктора разделов конфигурации с открытым исходным кодом на CodePlex. Это позволяет создавать настраиваемые разделы конфигурации с помощью дизайнера, размещенного в Visual Studio.
Например, дизайн пользовательского раздела конфигурации выглядит так:
приведет к файлу конфигурации, как это:
<?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);
}
Для всех, кто нашел этот ответ, как и я, я уточнил ответ, чтобы использовать более стандартные части разметки ConfigurationManager, чтобы уменьшить количество требуемого стандартного кода:
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace TestSite
{
public class SiteConfiguration : ConfigurationSection
{
[ConfigurationProperty("listValues", DefaultValue = null, IsRequired = true)]
[ConfigurationCollection(typeof(ListValues),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public ListValues ListValues
{
get { return (ListValues)this["listValues"]; }
set { this["listValues"] = value; }
}
}
/// <summary>
/// Boilder plate holder for the collection of values
/// </summary>
public class ListValues : ConfigurationElementCollection, IEnumerable<ConfigurationElement>
{
protected override ConfigurationElement CreateNewElement() { return new ListElement(); }
protected override object GetElementKey(ConfigurationElement element)
{
return ((ListElement)element).Value;
}
IEnumerator<ConfigurationElement> IEnumerable<ConfigurationElement>.GetEnumerator()
{
return this.OfType<ListElement>().GetEnumerator();
}
}
/// <summary>
/// Boilder plate holder for each value
/// </summary>
public class ListElement : ConfigurationElement
{
[ConfigurationProperty("value")]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}
С соответствующим web.config:
<configSections>
<section name="siteConfiguration" type="TestSite.SiteConfiguration, TestSite"/>
</configSections>
<siteConfiguration>
<listValues>
<clear/>
<add value="one"/>
<add value="two"/>
<add value="three"/>
<add value="four"/>
<add value="five"/>
</listValues>
</siteConfiguration>
Который затем можно использовать так:
List<string> list = new List<string>();
ListValues values = ((SiteConfiguration)ConfigurationManager.GetSection("siteConfiguration")).ListValues;
foreach (ListElement elem in values)
{
list.Add(elem.Value);
}
И вуаля, все значения теперь в списке.(Протестировано в .Net Framework 4.8)