Tilak
Tilak

Reputation: 30728

How to create nested custom configuration section in c#?

I need custom configuration in following way

<root>
<group>
 <groupitem> 
    <property1/>
    <property2/>
    <property3/>
    <property4/>   
 </groupitem>
<group>
</root>

I am not finding any example of how to do that.

Following comes very close,

Nested Configuration Section app.config

but still I am stuck on defining class corresponding to groupitem. If I change property1, property2 from element to attribute, that is easy to define.

But that will create problem if these properties have nested properties.

Question is, How to define the nested hierarchy in the way defined above?

Upvotes: 6

Views: 6949

Answers (1)

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64943

The configuration section is the top-level element of some configuration model. For example, your product name would be the configuration section.

Any other nested element is a ConfigurationElement or ConfigurationElementCollection.

Actually you can also create custom section groups deriving ConfigurationSectionGroup.

Taking your configuration:

<root> <!-- This is a ConfigurationSection -->
<group><!-- This is a ConfigurationElement -->
 <groupitem> <-- This could be a ConfigurationElement having child items of type ConfigurationElement or a ConfigurationElementCollection -->
    <property1/> <!-- This is a ConfigurationElement -->
    <property2/> <!-- This is a ConfigurationElement -->
    <property3/> <!-- This is a ConfigurationElement -->
    <property4/> <!-- This is a ConfigurationElement -->   
 </groupitem>
<group>

If you really want to know how to work with .NET configuration model, take a look at these articles:

Upvotes: 6

Related Questions