views:

28

answers:

0

I need merge a section of type NameValueSectionHandler from two .config files (Main.config and Secundary.config in sample).

The Main.config has another sections like appsetings and others.

Main.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ParserSQL.Reglas" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <ParserSQL.Reglas>
    <add key="Rule1" value="ParserSQL.Reglas.Rule1,ParserSQL.Rule1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule2" value="ParserSQL.Reglas.Rule2,ParserSQL.Rule2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
  </ParserSQL.Reglas> 

<!-- other sections here -->

</configuration>

Secundary.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

 <configSections>
    <section name="ParserSQL.Reglas" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <ParserSQL.Reglas>

    <add key="Rule1" value="ParserSQL.Reglas.Rule1,ParserSQL.Rule1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule3" value="ParserSQL.Reglas.Rule3,ParserSQL.Rule3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule4" value="ParserSQL.Reglas.Rule4,ParserSQL.Rule4, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>

  </ParserSQL.Reglas>

</configuration>

After merge process, the final config (Main.config modified)

Main.config modified

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ParserSQL.Reglas" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <ParserSQL.Reglas>
    <add key="Rule1" value="ParserSQL.Reglas.Rule1,ParserSQL.Rule1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule2" value="ParserSQL.Reglas.Rule2,ParserSQL.Rule2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule3" value="ParserSQL.Reglas.Rule3,ParserSQL.Rule3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
    <add key="Rule4" value="ParserSQL.Reglas.Rule4,ParserSQL.Rule4, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744"/>
  </ParserSQL.Reglas>


<!-- other sections here -->


</configuration>

Any suggestions or sample code in C# for best easy way using Xml.Linq (XDocument class) ?