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
ConfigFileManager config = new ConfigFileManager(DistribConfigFile,
ExistingConfigFile,
true);
config.ReplaceXPathValues(
"//@value",
"QAServer/QAVirtDir1",
new Regex(@"http://(.*)/service.ashx") );
config.ReplaceXPathValues(
"/configuration/appSettings/add[@key='param2']/@value",
"QA-value2");
config.Save();
Example C# Code within a NAnt script
<!---->
<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);
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.