Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:20:14+00:00 2026-06-13T22:20:14+00:00

I need a configuration section something like this: <myConfig> <mySubConfig1> <mySubSubConfig1 keyAttribute=value1> <mySubSubConfig1Element keyAttribute=value1/>

  • 0

I need a configuration section something like this:

<myConfig>
    <mySubConfig1>
        <mySubSubConfig1 keyAttribute="value1">
            <mySubSubConfig1Element keyAttribute="value1"/>
            <mySubSubConfig1Element keyAttribute="value2"/>
        </mySubSubConfig1>
        <mySubSubConfig1 keyAttribute="value2">
            <mySubSubConfig1Element keyAttribute="value1"/>
        </mySubSubConfig1>
    </mySubConfig1>
    <mySubConfig2>
        <mySubSubConfig2 keyAttribute="value1">
            <mySubSubConfig2Element keyAttribute="value1"/>
            <mySubSubConfig2Element keyAttribute="value2"/>
        </mySubSubConfig2>
        <mySubSubConfig2 keyAttribute="value2">
            <mySubSubConfig2Element keyAttribute="value1"/>
        </mySubSubConfig2>
    </mySubConfig2>
    <mySubConfig3>
        <mySubSubConfig3 keyAttribute="value1">
            <mySubSubConfig3Element keyAttribute="value1"/>
            <mySubSubConfig3Element keyAttribute="value2"/>
        </mySubSubConfig3>
        <mySubSubConfig3 keyAttribute="value2">
            <mySubSubConfig3Element keyAttribute="value1"/>
        </mySubSubConfig3>
    </mySubConfig3>
</myConfig>

I haven’t yet found the magic that would permit this withoug using the old IConfigurationSectionHandler interface. Does anyone know how to do it?

It’s ok with me if myConfig and the mySubConfign are ConfigurationSectionGroup or ConfigurationSection.

Also, if it matters, this will be used from web.config.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:20:16+00:00Added an answer on June 13, 2026 at 10:20 pm

    In your example config file, myConfig would be a class that inherits from ConfigurationSection with three properties named mySubConfig1, mySubConfig2 and mySubConfig3.

    The type of the mySubConfig1 property (as well as 2 and 3) would be a class that inherits from ConfigurationElementCollection, implements IEnumerable<ConfigElement> and is decorated with ConfigurationCollection (where the “AddItemName” property is set to “mySubSubConfig1”).

    Below is a complete sample implementation of an approach I used in a production deployment. Be sure to include the System.Configuration assembly. (It’s a bit confusing because the System.Configuration namespace is defined in other assmeblies, but you must include the System.Configuration assembly to use the code below.)

    Here are the custom configuration classes:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ConfigTest {
        class CustomConfigSection : ConfigurationSection {
    
            [ConfigurationProperty( "ConfigElements", IsRequired = true )]
            public ConfigElementsCollection ConfigElements {
                get {
                    return base["ConfigElements"] as ConfigElementsCollection;
                }
            }
    
        }
    
        [ConfigurationCollection( typeof( ConfigElement ), AddItemName = "ConfigElement" )]
        class ConfigElementsCollection : ConfigurationElementCollection, IEnumerable<ConfigElement> {
    
            protected override ConfigurationElement CreateNewElement() {
                return new ConfigElement();
            }
    
            protected override object GetElementKey( ConfigurationElement element ) {
                var l_configElement = element as ConfigElement;
                if ( l_configElement != null )
                    return l_configElement.Key;
                else
                    return null;
            }
    
            public ConfigElement this[int index] {
                get {
                    return BaseGet( index ) as ConfigElement;
                }
            }
    
            #region IEnumerable<ConfigElement> Members
    
            IEnumerator<ConfigElement> IEnumerable<ConfigElement>.GetEnumerator() {
                return ( from i in Enumerable.Range( 0, this.Count )
                         select this[i] )
                        .GetEnumerator();
            }
    
            #endregion
        }
    
        class ConfigElement : ConfigurationElement {
    
            [ConfigurationProperty( "key", IsKey = true, IsRequired = true )]
            public string Key {
                get {
                    return base["key"] as string;
                }
                set {
                    base["key"] = value;
                }
            }
    
            [ConfigurationProperty( "SubElements" )]
            public ConfigSubElementsCollection SubElements {
                get {
                    return base["SubElements"] as ConfigSubElementsCollection;
                }
            }
    
        }
    
        [ConfigurationCollection( typeof( ConfigSubElement ), AddItemName = "ConfigSubElement" )]
        class ConfigSubElementsCollection : ConfigurationElementCollection, IEnumerable<ConfigSubElement> {
    
            protected override ConfigurationElement CreateNewElement() {
                return new ConfigSubElement();
            }
    
            protected override object GetElementKey( ConfigurationElement element ) {
                var l_configElement = element as ConfigSubElement;
                if ( l_configElement != null )
                    return l_configElement.Key;
                else
                    return null;
            }
    
            public ConfigSubElement this[int index] {
                get {
                    return BaseGet( index ) as ConfigSubElement;
                }
            }
    
            #region IEnumerable<ConfigSubElement> Members
    
            IEnumerator<ConfigSubElement> IEnumerable<ConfigSubElement>.GetEnumerator() {
                return ( from i in Enumerable.Range( 0, this.Count )
                         select this[i] )
                        .GetEnumerator();
            }
    
            #endregion
        }
    
        class ConfigSubElement : ConfigurationElement {
    
            [ConfigurationProperty( "key", IsKey = true, IsRequired = true )]
            public string Key {
                get {
                    return base["key"] as string;
                }
                set {
                    base["key"] = value;
                }
            }
    
        }
    
    
    }
    

    Here’s the App.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="CustomConfigSection" type="ConfigTest.CustomConfigSection,ConfigTest" />
      </configSections>
    
      <CustomConfigSection>
        <ConfigElements>
          <ConfigElement key="Test1">
            <SubElements>
              <ConfigSubElement key="-SubTest1.1" />
              <ConfigSubElement key="-SubTest1.2" />
            </SubElements>
          </ConfigElement>
          <ConfigElement key="Test2">
            <SubElements>
              <ConfigSubElement key="-SubTest2.1" />
              <ConfigSubElement key="-SubTest2.2" />
            </SubElements>
          </ConfigElement>
        </ConfigElements>
      </CustomConfigSection>
    
    </configuration>
    

    Finally, here’s the code which accesses and uses the config file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    
    namespace ConfigTest {
        class Program {
            static void Main( string[] args ) {
    
                var l_configSettings = (CustomConfigSection) ConfigurationManager.GetSection( "CustomConfigSection" );
    
                foreach ( var l_element in l_configSettings.ConfigElements.AsEnumerable() ) {
                    Console.WriteLine( l_element.Key );
    
                    foreach ( var l_subElement in l_element.SubElements.AsEnumerable() ) {
                        Console.WriteLine( l_subElement.Key );
                    }
    
                }
    
                Console.WriteLine( "Press any key..." );
                Console.ReadKey( true );
    
            }
        }
    }
    

    A lighter-weight alternative was written by Sunil Singh on his blog:
    http://blogs.quovantis.com/net-creating-a-custom-configuration-section-that-contains-a-collection-of-collections/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've got a ConfigurationSection/ConfigurationElementCollection that has a configuration like this: <mimeFormats> <add mimeFormat=text/html
I need to create a configuration section, that is able to store key-value pairs
I need to update a value in one of my custom configuration section in
I am using ClickOnce deployment for a Windows Forms application and I need configuration
I need to pass configuration settings to an AddIn created using the Managed AddIn
I need a .htaccess configuration that does the following: Redirect www.domain.com to domain.com Redirect
I need to build a configuration panel in the Plone control panel that store
I need to decide which configuration framework to use. At the moment I am
i need to change the configuration of php.ini on server side (parameter is allow_url_fopen)
I need to implement a configuration file, which should be rescanned periodically or after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.