I assume that the collection
is a property of your custom ConfigurationSection
class.
You can decorate this property with the following attributes:
[ConfigurationProperty("", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(MyElementCollection), AddItemName = "element")]
A full implementation for your example could look like this:
public class MyCustomSection : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(MyElementCollection), AddItemName = "element")]
public MyElementCollection Elements
{
get { return (MyElementCollection)this[""]; }
}
}
public class MyElementCollection : ConfigurationElementCollection, IEnumerable<MyElement>
{
private readonly List<MyElement> elements;
public MyElementCollection()
{
this.elements = new List<MyElement>();
}
protected override ConfigurationElement CreateNewElement()
{
var element = new MyElement();
this.elements.Add(element);
return element;
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyElement)element).Name;
}
public new IEnumerator<MyElement> GetEnumerator()
{
return this.elements.GetEnumerator();
}
}
public class MyElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
}
}
Now you can access your settings like this:
var config = (MyCustomSection)ConfigurationManager.GetSection("customSection");
foreach (MyElement el in config.Elements)
{
Console.WriteLine(el.Name);
}
This will allow the following configuration section:
<customSection>
<element name="A" />
<element name="B" />
<element name="C" />
<customSection>