Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge and override .NET configuration files with C#
    text
    copied!<p>I wrote this piece of code to merge two .NET configuration files: Add non existent nodes and override values on existing ones. I avoided to use the much faster reader/writer way because the app I'm using it for is not performance critical. What do you think of this?</p> <pre><code>using System; using System.Xml; namespace devcoach.FrameworkExtensions { /// &lt;summary&gt; /// Extension methods for the type /// &lt;see cref="System.Xml.XmlDocument"/&gt;. /// &lt;/summary&gt; public static class XmlDocumentExtensions { /// &lt;summary&gt; /// Merges the specified instance. /// &lt;/summary&gt; /// &lt;param name="instance"&gt;The instance.&lt;/param&gt; /// &lt;param name="mergeDoc"&gt;The merge doc.&lt;/param&gt; public static void Merge( this XmlDocument instance, XmlDocument mergeDoc) { var mergeRoot = mergeDoc.DocumentElement; var sourceRoot = instance.DocumentElement; if (sourceRoot == null) return; instance.Merge(sourceRoot, mergeRoot, false); } /// &lt;summary&gt; /// Merges the specified instance. /// &lt;/summary&gt; /// &lt;param name="instance"&gt;The instance.&lt;/param&gt; /// &lt;param name="mergeDoc"&gt;The merge doc.&lt;/param&gt; /// &lt;param name="isNetConfigFile"&gt;if set to &lt;c&gt;true&lt;/c&gt; if documents /// are .net config files.&lt;/param&gt; public static void Merge( this XmlDocument instance, XmlDocument mergeDoc, bool isNetConfigFile) { var mergeRoot = mergeDoc.DocumentElement; var sourceRoot = instance.DocumentElement; if (sourceRoot == null) return; instance.Merge(sourceRoot, mergeRoot, true); } /// &lt;summary&gt; /// Merges the specified source doc. /// &lt;/summary&gt; /// &lt;param name="sourceDoc"&gt;The source doc.&lt;/param&gt; /// &lt;param name="sourceRoot"&gt;The source root.&lt;/param&gt; /// &lt;param name="mergeRoot"&gt;The merge root.&lt;/param&gt; public static void Merge( this XmlDocument sourceDoc, XmlNode sourceRoot, XmlNode mergeRoot) { sourceDoc.Merge( sourceRoot, mergeRoot, false); } /// &lt;summary&gt; /// Merges the specified source doc. /// &lt;/summary&gt; /// &lt;param name="sourceDoc"&gt;The source doc.&lt;/param&gt; /// &lt;param name="sourceRoot"&gt;The source root.&lt;/param&gt; /// &lt;param name="mergeRoot"&gt;The merge root.&lt;/param&gt; /// &lt;param name="isNetConfigFile"&gt;if set to &lt;c&gt;true&lt;/c&gt; if documents /// are .net config files.&lt;/param&gt; public static void Merge( this XmlDocument sourceDoc, XmlNode sourceRoot, XmlNode mergeRoot, bool isNetConfigFile) { #region Check parameters and if needed throw ArgumentNullException if (sourceDoc == null) { throw new ArgumentNullException("sourceDoc"); } if (sourceRoot == null) { throw new ArgumentNullException("sourceRoot"); } if (mergeRoot == null) { throw new ArgumentNullException("mergeRoot"); } #endregion if (!string.IsNullOrEmpty(mergeRoot.InnerText)) { sourceRoot.InnerText = mergeRoot.InnerText; } if (!string.IsNullOrEmpty(mergeRoot.Value)) { sourceRoot.Value = mergeRoot.Value; } #region Copy attributes... var mergeRootAttributes = mergeRoot.Attributes; if (mergeRootAttributes != null) { var mergeRootAttributesCount = mergeRootAttributes.Count; for (var k = 0; k &lt; mergeRootAttributesCount; k++) { var mergeAttribute = mergeRootAttributes[k]; var mergeAttributeName = mergeAttribute.LocalName; var sourceAttribute = sourceRoot.Attributes[mergeAttributeName] ?? sourceRoot.Attributes.Append( sourceDoc.CreateAttribute( mergeAttribute.Prefix, mergeAttribute.LocalName, mergeAttribute.NamespaceURI)); sourceAttribute.Value = mergeAttribute.Value; } } #endregion // loop child nodes... var mergeRootNodesCount = mergeRoot.ChildNodes.Count; for (var i = 0; i &lt; mergeRootNodesCount; i++) { XmlNode foundNode = null; var mergeNode = mergeRoot.ChildNodes[i]; var mergeNodeName = mergeNode.LocalName; var sourceRootNodeCount = sourceRoot.ChildNodes.Count; #region Find node in source... for (var j = 0; j &lt; sourceRootNodeCount; j++) { var sourceNode = sourceRoot.ChildNodes[j]; var sourceNodeName = sourceNode.LocalName; if ((isNetConfigFile &amp;&amp; mergeNodeName == "section" || mergeNodeName == "sectionGroup" ) || sourceNodeName != mergeNodeName) continue; foundNode = sourceNode; break; } #endregion #region create a new node... if (foundNode == null) { foundNode = sourceRoot.AppendChild( sourceDoc.CreateNode( mergeNode.NodeType, mergeNode.Prefix, mergeNode.LocalName, mergeNode.NamespaceURI)); } #endregion sourceDoc.Merge(foundNode, mergeNode); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload