Click here to Skip to main content
13,409,394 members (39,545 online)
Click here to Skip to main content
Add your own
alternative version


59 bookmarked
Posted 13 Jun 2004

XmlConfigMerge - Merge config file settings

, 6 Nov 2006
Rate this:
Please Sign up or sign in to vote.
Supports merging of .config file settings, such as in a push to staging-server deployment environment. Also supports XPath updates. Class library and Console app provided.


When deploying .NET applications, it is often desirable to "merge" configuration settings between a "distribution" .config file and an existing .config file.

This is particularly useful when new config parameters are added to a "distribution" config file, and an existing .config file (such as on an already-configured server) must have these new parameters added, while preserving existing parameter settings.

The XmlConfigMerge utility (provided both as a library assembly and as a console application) supports this merging of XML .config files, and also supports setting of specific parameters via XPath filtering and optional Regex pattern matching.

Example Usage Scenarios

  • Code Promotion: Push release from development to staging: Use to add newly-added development parameters while preserving existing staging parameters. (See "Example C# Code" below.)
  • Publisher Policy Files: Set the binding redirect parameters such that the AssemblyVersion in the latest build is specified (see "Example C# Code within a NAnt script" below.)
  • Windows Installer Packages: Maintain existing application .config settings (from a prior install).

Example C# Code

//Merge an "existing" config's settings into a "distrib" config, 
//writing out results to "existing" config
ConfigFileManager config = new ConfigFileManager(DistribConfigFile,
        true); //makeMergeFromConfigPathTheSavePath

//Regex pattern replace having single regex group to be replaced within
//pattern, matching multiple config items
        new Regex(@"http://(.*)/service.ashx") );

//appsettings key-value pair set for existing param. 
//It is created if it does not exist.


Example C# Code within a NAnt script

<!-- Set the publisher-policy file newVersion attribute -->

<property value="policyTheAssemblyName.xml" name="policyFileName"/>
<property value="" name="policyAssemblyVersion"/>
<script language="C#">
        public static void ScriptMain(Project project) {
            policyFileName = project.Properties["policyFileName"];
            project.Log(Level.Info, "Modifying policy file: " + 

            ConfigFileManager config = 
                  new ConfigFileManager(policyFileName);
            LogInfoLines(project, config.ReplaceXPathValues(
                    project.Properties["policyAssemblyVersion"] ) );
        private static void LogInfoLines(Project project, 
                                         string[] lines) {
            foreach (string line in lines) {
                project.Log(Level.Info, line);
        <includes name="${nant.location}\XmlConfigMerge.dll"/>
        <import name="Tools.XmlConfigMerge"/>

Example Command-Line

XmlConfigMergeConsole.exe "..\MergeDistribConfigFiles\web.config" 
-m "..\MergeExistingConfigFiles\web.config" -mwritten
-r "//@value" "QAServer/QAVirtDir1" "http://(.*)/service.ashx"
-r "/configuration/appSettings/add[@key='param2']/@value" "QA-value2"

Further Examples

See the included unit-tests (utilizing NUnit) for additional usage examples.

Release Notes

  • Version 1.3 - 11/06/2006: Changed to use share mode on file opens, added error handling.
  • Version 1.2: Fix for master child nodes not being preserved when not present in merge-from doc.
  • Version 1.1: Handles duplicate appSettings keys in a manner consistent with System.Configuration (uses last one); added precondition checks.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
.NET solution architect / developer based in Chicago.

Check out the InRule .NET Business Rule Engine - 30 day trial download available:

You may also be interested in...

Comments and Discussions

QuestionLicensing Pin
rafik198227-Mar-17 8:58
memberrafik198227-Mar-17 8:58 
QuestionLicensing question Pin
Member 109324338-Jul-14 3:56
memberMember 109324338-Jul-14 3:56 
AnswerRe: Licensing question Pin
martinhughesireland224-Nov-16 5:12
membermartinhughesireland224-Nov-16 5:12 
GeneralRe: Licensing question Pin
rafik198227-Mar-17 9:47
memberrafik198227-Mar-17 9:47 
QuestionHelpful tool Pin
mmsharp11-Nov-13 11:04
membermmsharp11-Nov-13 11:04 
AnswerRe: Helpful tool Pin
rafik198227-Mar-17 9:47
memberrafik198227-Mar-17 9:47 
QuestionVery good useful tool! Pin
David O. Lavallee9-Dec-11 8:10
memberDavid O. Lavallee9-Dec-11 8:10 
AnswerRe: Very good useful tool! Pin
rafik198227-Mar-17 9:47
memberrafik198227-Mar-17 9:47 
QuestionVery useful tool! Pin
David O. Lavallee9-Dec-11 8:00
memberDavid O. Lavallee9-Dec-11 8:00 
AnswerRe: Very useful tool! Pin
rafik198227-Mar-17 9:48
memberrafik198227-Mar-17 9:48 
GeneralDid you hear something about DotNet.Helper Project Pin
void leenux();15-Apr-11 3:04
membervoid leenux();15-Apr-11 3:04 
GeneralThanks a lot! Pin
humanier26-Aug-10 4:47
memberhumanier26-Aug-10 4:47 
QuestionUsing this at runtime, how do you refresh the config file to reflect the changes? Pin
Leon v Wyk10-Oct-08 4:05
memberLeon v Wyk10-Oct-08 4:05 
QuestionNAnt and msi's? Pin
robrich6-Nov-06 10:32
memberrobrich6-Nov-06 10:32 
GeneralXML Diff and Patch GUI Tool Pin
Christian Birkl26-Sep-06 8:17
memberChristian Birkl26-Sep-06 8:17 
GeneralGood tool Pin
Mark Focas29-Jun-04 20:41
memberMark Focas29-Jun-04 20:41 
GeneralAvoiding problems with schemas Pin
Ariel Erenberg28-Jun-04 1:31
sussAriel Erenberg28-Jun-04 1:31 
GeneralRe: Avoiding problems with schemas Pin
buc3-Jul-04 10:57
memberbuc3-Jul-04 10:57 
GeneralRe: Avoiding problems with schemas Pin
thanigaivelanb23-Jan-09 0:03
memberthanigaivelanb23-Jan-09 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180221.1 | Last Updated 6 Nov 2006
Article Copyright 2004 by buc
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid