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>
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>
source share