So, I'm trying to make a custom configuration section for my app.config that will process the following pattern:
<domainSolutionSection>
<domainSolutionGroups>
<Group groupname="Legacy">
<add name="Old App #1"/>
<add name="Old App #2" />
</Group>
<Group groupname="Modern">
<add name="New App #1" />
<add name="New App #2" />
<add name="New App #3" />
</Group>
</domainSolutionGroups>
</domainSolutionSection>
I've spent all day trying to get this to work.
Right now, I'm getting an error:
System.Configuration.ConfigurationErrorsException: Unrecognized element 'add'.
Here are the classes in my DomainSolutionSection library:
public class DomainSolutionSection : ConfigurationSection
{
[ConfigurationProperty("domainSolutionGroups")]
public DomainSolutionGroupCollection DomainSolutionGroups
{
get
{
return (DomainSolutionGroupCollection) this["domainSolutionGroups"];
}
set
{
this["domainSolutionGroups"] = value;
}
}
}
[ConfigurationCollection(typeof(GroupConfigElement))]
public class DomainSolutionGroupCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new GroupConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((GroupConfigElement)element).GroupName;
}
protected override string ElementName
{
get { return "Group"; }
}
protected override bool IsElementName(string elementName)
{
return !String.IsNullOrEmpty(elementName) && elementName == "Group";
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public GroupConfigElement this[int index]
{
get
{
return base.BaseGet(index) as GroupConfigElement;
}
}
public new GroupConfigElement this[string key]
{
get
{
return base.BaseGet(key) as GroupConfigElement;
}
}
}
public class GroupConfigElement : ConfigurationElement
{
[ConfigurationProperty("groupname", IsRequired = true, IsKey = true)]
public string GroupName
{
get { return (string)this["groupname"]; }
set { this["groupname"] = value; }
}
public PhraseCollection Phrases
{
get { return (PhraseCollection) base["Groups"]; }
}
}
[ConfigurationCollection(typeof(PhraseConfigElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class PhraseCollection: ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new PhraseConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((PhraseConfigElement)element).Name;
}
}
public class PhraseConfigElement: ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
Not finding any examples out there where "add" properties are used at this level of nesting, so obviously, I haven't "wired-up" the "Group" properly. Can anybody offer any guidance?
Thanks!