I am currently stuck into the problem of how to get all configuration properties out of the ConfigurationElementCollection object. In this post I have printed out the sources for the customized config-file and also the sources for the config-class, which handles the access to the given customized config-file.
configuration>
configSections>
section name="Section1" type="MyProject.Section1, Section1" />
/configSections>
Section1>
Section1FieldDefinitions>
add id="1" project="Proj1" browsername="Order1"/>
add id="2" project="Proj2" browsername="Order2"/>
add id="3" project="Proj3" browsername="Order3"/>
/Section1FieldDefinitions>
/Section1>
/configuration>
Source of project.config file. I had to remove the xml open bracket because I was not able to post that code with open brackets.
public class Section1 : ConfigurationSection
{
public const string SectionName = "Section1";
private const string FieldDefinitionsCollectionName = "Section1FieldDefinitions";
[ConfigurationProperty(FieldDefinitionsCollectionName)]
[ConfigurationCollection(typeof(Section1Collection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public Section1 Items
{
get
{
return (Section1Collection)base[FieldDefinitionsCollectionName];
}
}
}
public class Section1Collection : ConfigurationElementCollection
{
public Section1Collection()
{
}
public Section1FieldDefinitionsElement this[int index]
{
get
{
return (Section1FieldDefinitionsElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(SectionFieldDefinitionsElement pSection1)
{
BaseAdd(pSection1);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new Section1FieldDefinitionsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Section1FieldDefinitionsElement)element).ID;
}
public void Remove(Section1FieldDefinitionsElement pSection1)
{
BaseRemove(pSection1.ID);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
}
public class Section1FieldDefinitionsElement : ConfigurationElement
{
[ConfigurationProperty("id", IsRequired = true)]
public string ID
{
get
{
return (string)this["id"];
}
set
{
this["id"] = value;
}
}
[ConfigurationProperty("project", IsRequired = true)]
public string Project
{
get
{
return (string)this["project"];
}
set
{
this["project"] = value;
}
}
[ConfigurationProperty("browsername", IsRequired = true)]
public string Browsername
{
get
{
return (string)this["browsername"];
}
set
{
this["browsername"] = value;
}
}
}
Source of config class
What I currently need is a solution where I can iterate through all Section1FieldDefinitions and get the field names and the field values out the configuration properties. For example - we have three configuration elements - id, project and browsername. I need these three properties by name and value.