1

When I do this ConfigurationManager.GetSection("SectionA/sectionD") I receive this error:

Unrecognized element 'add'

I'd like read all "add" element from this section to create a collection.

<configSections>
    <sectionGroup name="SectionA">
        <sectionGroup name="SectionB">
            <section name="sectionC" type="MyProject.SectionC,MyProject" />
            <section name="sectionD" type="MyProject.SectionD,MyProject" />
        </sectionGroup>
        <section name="sectionE" type="MyProject.SectionE,MyProject" />
    </sectionGroup>
</configSections>

<SectionA>
    <SectionB>
        <sectionD>
            <add key="PerPage10" value="10" />
            <add key="PerPage30" value="30" />
            <add key="PerPage60" value="60" />
        </sectionD>
        <sectionC attribute3="10" />
    </SectionB>
    <sectionE attribute1="3" attribute2="5" />
</SectionA>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

1 Answers1

1

Here's an example of configuration element collection code delcaration:

private static ConfigurationProperty propIndicators = new ConfigurationProperty("indicators", typeof(ConfigurationElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);

[ConfigurationProperty("indicators", IsRequired = true, IsDefaultCollection = true)]
[ConfigurationCollection(typeof(ReferencedConfigurationElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public ConfigurationElementCollection Indicators
{
    get
    {
        return (ConfigurationElementCollection)this[propIndicators];
    }
    set
    {
        this[propIndicators] = value;
    }
}

So in config it looks as following:

<indicators>
    <add ... />
</indicators>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • What is the content of ConfigurationElementCollection ? – TheBoubou Nov 14 '11 at 11:28
  • @Kris-I: What is the definition of your SectionA, SectionB, SectionC? See http://stackoverflow.com/questions/7991252/read-web-config-section-to-list/7991452#7991452 – abatishchev Nov 14 '11 at 11:32
  • The problem is only SectionD, get all "add" value. I know the link but it's really really not clear for me. I'd like as result a collection with 10,30,60 ... a code welcome :) – TheBoubou Nov 14 '11 at 11:47
  • Oh! You're the author of that question. Sorry, I thought you're just person asking a similar question :)) – abatishchev Nov 14 '11 at 13:28
  • @Kris-I: An attribute of section SectionB? – abatishchev Nov 15 '11 at 07:14
  • Is it possible to add "" in SectionD ? (same level than add) or place "attribute3="10" in SectionD as attribute ? – TheBoubou Nov 16 '11 at 10:01
  • @Kris-I: You can declare add-action name, it can be "add", or "blah" or "sectionX". See [ConfigurationCollectionAttribute](http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx) and its usage above – abatishchev Nov 16 '11 at 10:14
  • @Kris-I: It's possible, e.g. `` – abatishchev Nov 16 '11 at 10:58
  • In this page http://goo.gl/diVMf I don't my case : an attribute in the section – TheBoubou Nov 16 '11 at 11:47
  • @Kris-I: Because you use `` you need to mark SectionD with `[ConfigurationCollection(typeof(SectionD), AddItemName = "add", ...`, also it have property MyAttribute. SectionD is a collection containing elements with properties: Key, Name. – abatishchev Nov 16 '11 at 13:03
  • Pffff completely lost :( How get "MyAttribute" ? the "add" is in the section me I'd like read an attribute of the collection (sectiongroup) – TheBoubou Nov 16 '11 at 13:37