title: Programmatic XML Diff / Merge in C#
body: <p>At this moment, I am managing a piece of software that has multiple XML configuration files. When a new version of software is released, sometimes the base config files change, we currently have the software call <a href="http://kdiff3.sourceforge.net/" rel="noreferrer">KDiff</a> on startup. If it detects a change, it prompts the user to choose the changes.</p>
  
  <p>The problem with this approach is that KDiff is a line comparing program and not aware of the ways of XML (like Nodes, etc.) </p>
  
  <p>Ideally, I would like to programmatically work with a library in C# (since we're a MS shop) that can Diff two XML files: a Source XML and a Current Working XML.</p>
  
  <p>And then Merge the two together using a few simple rules:</p>
  
  <ol>
  <li>If the Current Working XML has a node that the Source XML does not, remove it.</li>
  <li>If the Source XML has a node that the Current Working XML does not, add it.</li>
  <li>If both have the same node and the values differ, favor the Source XML's value, unless it the Source XML's value is set to "UseExistingValue".</li>
  </ol>
  
  <p>For example, here's the "Source" XML:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Items&gt;
       &lt;Item Id="1" Position="true"&gt;
         &lt;Location X="UseExistingValue" Y="UseExistingValue" Z="UseExistingValue" /&gt;
  
         &lt;Something/&gt;
         &lt;SomethingElse/&gt;
       &lt;/Item&gt;
     &lt;/Items&gt;
   &lt;/Configuration&gt;
  </code></pre>
  
  <p>And here's the "Current Working" XML:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Items&gt;
      &lt;Item Id="1" Position="false"&gt;
        &lt;Location X="123" Y="234" Z="345" /&gt;
        &lt;Another/&gt;
        &lt;Something/&gt;
  
      &lt;/Item&gt;
    &lt;/Items&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>And the merged version would look like:</p>
  
  <pre><code>&lt;Configuration&gt;
    &lt;Items&gt;
      &lt;Item Id="1" Position="true"&gt;
        &lt;Location X="123" Y="234" Z="345" /&gt;
  
        &lt;Something/&gt;
        &lt;SomethingElse/&gt;
      &lt;/Item&gt;
    &lt;/Items&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>I've looked at the <a href="http://msdn.microsoft.com/en-gb/library/aa302294.aspx" rel="noreferrer">MS XML Diff and Patch Tool</a> and it definitely merges the files together, but doesn't allow for the programmatic rules that I want to define.</p>
  
  <p><a href="http://xmlunit.sourceforge.net/" rel="noreferrer">XMLUnit for Java devs</a> seems promising, but the .NET version of it seems underdeveloped, which is unfortunate.</p>
  
  <p>Anyone have any suggestions for either scriptable XML Diff/Merge tools and/or .NET libraries (paid or free)?</p>
  
  <p>Thanks.</p>