How to read and write Hierarchical Configuration Settings

S

Satya

Hi,
I need to read and write Configuration Settings as shown below:

<MySettings>
<General>
<Color>Red</Color>
<.Font>TimesNewRoman</Font>
<Path>C</Path>
</General>

<Employee Type="Manager"> //More than one employee
<Function Type="Stationary"> //More than one function
<Department name="Department1">
<Line Color="Blue" Font="" HideWhenShrinked="true/false">
<LineEntry Color="Green" Font="Arial">
<Tag> Tag1 </Tag>
<.Pre> Pre1</Pre>
<Post> Post1</Post>
</LineEntry>
<LineEntry Font="Arial">
<Tag> Tag2</Tag>
<.Pre> Pre2</Pre>
<Post>Post2</Post>
</LineEntry>
</Line>
</Department>
</Funtion>

<Function Type="Projects">
<Department name="Department2">
<Line HideWhenShrinked="true/false">
<LineEntry Color="Green" Font="Arial">
<Tag> Tag1 </Tag>
<.Pre> Pre1</Pre>
<Post> Post1</Post>
</LineEntry>
<LineEntry Color="Yellow" Font="Arial">
<Tag> Tag2</Tag>
<.Pre> Pre2</Pre>
<Post>Post2</Post>
</LineEntry>
<LineEntry Color="Yellow" Font="Arial">
<Tag> Tag3</Tag>
<.Pre> Pre3</Pre>
<Post>Post3</Post>
</LineEntry>
</Line>
</Department>
</Function>

</Employee>
</MySettings>

There are two aspects here:
(1) The settings are hierarchical.
(2) When a setting is not specified for any element (eg. Color) I need
to use the setting from the parent element and so on.. If an element
has no parent element I need to get the setting from the General
section.

How can I do this? Can I do this using the new ConfigurationSection in
..Net 2.0 or do I need to use XML and parse the settings file on my own?

Thanks for your help,
Satya.
 
G

Gary Holbrook

We solved a similar problem by creating a configuration class. When we
start our application, we read a file containing the settings and
deserialize into an object. At shutdown, we serialize the object into a
file.

The nice thing about using the class is that 1) It works with
propertygrid 2) You can manipulate data inside the accessors
 
S

Satya

Hi Gary,
In the configuration class that you wrote did you use any of the
..NET configuration classes or did you use xml and parse the whole file
yourself.

thanks,
Satya.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top