Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 934
Tilak Avatar asked Jan 15 '13 08:01

Tilak


1 Answers

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:

  • http://www.codeproject.com/Articles/37776/Writing-a-complex-custom-configuration-section
  • http://www.codeproject.com/Articles/163081/Custom-Configuration-Sections-in-NET
like image 170
Matías Fidemraizer Avatar answered Sep 28 '22 05:09

Matías Fidemraizer