app/web config custom sections, collection elements with attributes

2.3k views Asked by At

I thought this would be something simple to do, as I have an xml section like:

<InterfaceDetails>
    <Interface name="TestInterface1">
        <Details data="xxxxxx" />
        <Details data="yyyyyy" />
    </Interface>
    <Interface name="TestInterface2">
        <Details data="zzzzzz" />
    </Interface>
</InterfaceDetails>

The section handler hooks up fine but it throws an error when reading the xml saying it cannot find the "name" attribute, now without putting a LOAD of code in here I have currently got the following classes:

  • InterfaceDetailsSection

The main section container, has a property called Interface which links to an InterfaceElementCollection.

  • InterfaceElementCollection

This is the element collection deriving class which should expose the name attribute and the details elements beneath. I have tried giving this class an attribute called name, and this seemed to work but then I got another error about the child elements.

  • DetailsElement

This contains the data attribute for the Details elements.

I want to be able to ideally pull out each interface, and then for each interface pull out the details, however for the life of me, even looking at the plethora of tutorials in this area none of them seem to cover having multiple children within multiple children, or if they do not have attributes on the collections.

Can anyone see any glaring mistakes or give me any pointers as to where im going wrong.

2

There are 2 answers

0
Grofit On

Custom config section containing collection

The above question was FINALLY the answer I was looking for, so incase anyone else has same issue as me trying to navigate the mysterious world of ConfigurationSections they can be pointed in the right direction.

0
Aghilas Yakoub On

You can try with this

    <configuration>

    <!-- Configuration section-handler declaration area. -->
      <configSections>
        <sectionGroup name="myCustomGroup">
          <section 
            name="myCustomSection" 
            type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
            allowLocation="true" 
            allowDefinition="Everywhere"
          />
        </sectionGroup>
          <!-- Other <section> and <sectionGroup> elements. -->
      </configSections>

      <!-- Configuration section settings area. -->



 <myCustomGroup>
    <myCustomSection myAttrib1="Clowns">

    </myCustomSection>
  </myCustomGroup>


    </configuration>