Click here to Skip to main content
Licence 
First Posted 13 Jun 2004
Views 60,708
Downloads 237
Bookmarked 48 times

XmlConfigMerge - Merge config file settings

By | 6 Nov 2006 | Article
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.

Introduction

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,
        ExistingConfigFile,
        true); //makeMergeFromConfigPathTheSavePath

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

//appsettings key-value pair set for existing param. 
//It is created if it does not exist.
config.ReplaceXPathValues(
        "/configuration/appSettings/add[@key='param2']/@value",
        "QA-value2");

config.Save();

Example C# Code within a NAnt script

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

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

            //Modify
            ConfigFileManager config = 
                  new ConfigFileManager(policyFileName);
            LogInfoLines(project, config.ReplaceXPathValues(
                    "//*[local-name()='bindingRedirect']/@newVersion",
                    project.Properties["policyAssemblyVersion"] ) );
            config.Save();
        }
                
        private static void LogInfoLines(Project project, 
                                         string[] lines) {
            foreach (string line in lines) {
                project.Log(Level.Info, line);
            }
        }
    ]]></code>
    <references>
        <includes name="${nant.location}\XmlConfigMerge.dll"/>
    </references>
    <imports>
        <import name="Tools.XmlConfigMerge"/>
    </imports>
</script>

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.

License

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

buc

Web Developer

United States United States

Member

.NET solution architect / developer based in Chicago.

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

www.inrule.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVery good useful tool! PinmemberDavid O. Lavallee7:10 9 Dec '11  
QuestionVery useful tool! PinmemberDavid O. Lavallee7:00 9 Dec '11  
GeneralDid you hear something about DotNet.Helper Project Pinmembervoid leenux();2:04 15 Apr '11  
GeneralThanks a lot! Pinmemberhumanier3:47 26 Aug '10  
QuestionUsing this at runtime, how do you refresh the config file to reflect the changes? PinmemberLeon v Wyk3:05 10 Oct '08  
QuestionNAnt and msi's? Pinmemberrobrich9:32 6 Nov '06  
GeneralXML Diff and Patch GUI Tool PinmemberChristian Birkl7:17 26 Sep '06  
GeneralGood tool PinmemberMark Focas19:41 29 Jun '04  
GeneralAvoiding problems with schemas PinsussAriel Erenberg0:31 28 Jun '04  
GeneralRe: Avoiding problems with schemas Pinmemberbuc9:57 3 Jul '04  
GeneralRe: Avoiding problems with schemas Pinmemberthanigaivelanb23:03 22 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 6 Nov 2006
Article Copyright 2004 by buc
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid