Read the web.config section for a list

I have this in the web.config file:

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

I would like to read the entire section of "MySection" and get the whole value of List<string> (for example: "10", "20", "30")

Thanks,

+4
source share
2 answers

First of all, I recommend using Unity Configuration .

code:

 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> 
+5
source

I would recommend you take a look at the excellent open source Design Designer project on CodePlex. It allows you to create custom configuration sections using a designer hosted in Visual Studio.

For example, a custom configuration configuration section is as follows:

Simple Custom Section will create the configuration file as follows:

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

which can be used programmatically as follows:

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

Source: https://habr.com/ru/post/1379429/


All Articles