Merging two configuration files

  • Thread starter Thread starter Houda
  • Start date Start date
H

Houda

Here what I've been trying to do:
I've two different configuration files with the same structure. One of
them is the default setting, which gets downloaded from the server,
and the other one is the user settings. The user settings overwrite
the default settings. Similar to what .Net does with the
machine.config file and the web.config. Now I need to merge these two
files to get the actual settings.

The default settings file:

<bookmarks>
<add name="b1" lat="1" long="1"/>
<add name="b2" lat="1" long="1"/>
<add name="b3" lat="1" long="1"/>
<bookmarks/>


The user settings file:

<bookmarks>
<remove name="b1" lat="1" long="1"/>
<add name="b4" lat="1" long="1"/>
<bookmarks/>

The result after merging these two files should be:

<bookmarks>
<add name="b2" lat="1" long="1"/>
<add name="b3" lat="1" long="1"/>
<add name="b4" lat="1" long="1"/>
<bookmarks/>


Any ideas how to do this? If you think I should post this message to
another group let me know?

Thanks
Houda
 
Hi Houda,
The user settings overwrite
the default settings. Similar to what .Net does with the
machine.config file and the web.config. Now I need to merge these two
files to get the actual settings.

There's nothing automatic in .NET for this, so your best bet is to first
read the default settings into memory (into some kind of collection object
perhaps), and then read the user settings.

If you use the "name" attribute as a key, then for each line you load from
the user's settings, check to see if a setting with a similar name already
exists in your collection, and then act accordingly.

In the end, you have effectively merged the two XML files. Then, you can
process the collection's data or write the data out as a new XML file, if
that is what you want to do.

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top