<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aneef.Net &#187; XML</title>
	<atom:link href="http://www.aneef.net/category/tech/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aneef.net</link>
	<description>Do it in .Net way &#124; Blogging about C#,ASP.Net, LINQ,WPF and .Net Technologies</description>
	<lastBuildDate>Tue, 29 Nov 2011 13:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Compare and update XML File with the changes</title>
		<link>http://www.aneef.net/2008/10/30/compare-and-update-xml-file-with-the-changes/</link>
		<comments>http://www.aneef.net/2008/10/30/compare-and-update-xml-file-with-the-changes/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 08:46:50 +0000</pubDate>
		<dc:creator>Aneef Fashir</dc:creator>
				<category><![CDATA[.Net Related]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.aneef.net/2008/10/30/compare-and-update-xml-file-with-the-changes/</guid>
		<description><![CDATA[Recently I had a requirement to compare 2 XML Files, which has the existing and new settings of an application stored in them. what I wanted to do is when a new XML file of settings is arrived, I had to compare and retain the existing settings, and add any new ones if there are [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Recently I had a requirement to compare 2 XML Files, which has the existing and new settings of an application stored in them. what I wanted to do is when a new XML file of settings is arrived, I had to compare and retain the existing settings, and add any new ones if there are any, remove any settings if they are removed in the new one. Mainly I needed to compare nodes as well as attributes.</p>
<p>Here are few suggestion I got from my friends and communities:</p>
<ol>
<li><a href="http://msdn.microsoft.com/en-us/library/aa302294.aspx" target="_blank">XML Diff Patch</a> &#8211; Couldn&#8217;t achieve what I wanted. </li>
<li><a href="http://msdn.microsoft.com/en-us/library/bb387098.aspx" target="_blank">Linq for XML</a> -&#160; I cannot use this as the application is developed under .net Framework 2.0 </li>
</ol>
<p>&#160;</p>
<p>So after Several Discussions and Forum Posts I Decided to Write one on my own. I don&#8217;t know whether it is the best, but it servers for my purpose. so just thought of sharing it with you all, as I saw there were a lot with the same problem. I know&#160; it needs some improvements such as comparing nested nodes and attributes. but&#160; this would be a better kick start. &quot;something is better than nothing&quot; right!!!.</p>
<p>&#160;</p>
<p>Here are two sample XML files which we can use for our comparison, and the output expected.</p>
<p>&#160;</p>
<p><u>Existing Settings</u></p>
<pre class="csharpcode">&lt;Properties xmlns:xsi=<span class="str"><a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a></span></pre>
<pre class="csharpcode"><span class="str"></span> xmlns:xsd=<span class="str">&quot;http://www.w3.org/2001/XMLSchema&quot;</span>&gt;
    &lt;Property Name=<span class="str">&quot;PerformanceType1&quot;</span> ProID=<span class="str">&quot;1&quot;</span> Text=<span class="str">&quot;Gross&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;On|Off&quot;</span>&gt;On&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark1&quot;</span> ProID=<span class="str">&quot;3&quot;</span> Text=<span class="str">&quot;Benchmark 1&quot;</span> Type=<span class="str">&quot;ddlb&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark2&quot;</span> ProID=<span class="str">&quot;4&quot;</span> Text=<span class="str">&quot;Benchmark 2&quot;</span> Type=<span class="str">&quot;ddlb&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Region1&quot;</span> ProID=<span class="str">&quot;7&quot;</span> Text=<span class="str">&quot;Column 1&quot;</span> Type=<span class="str">&quot;ddlb&quot;</span> </pre>
<pre class="csharpcode">Values=<span class="str">&quot;N/A|1 Month|2 Month|3&quot;</span>&gt;1 Month&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;FromDate1&quot;</span> ProID=<span class="str">&quot;8&quot;</span> Text=<span class="str">&quot;Column 1 From&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;Date&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;FromDate9&quot;</span> ProID=<span class="str">&quot;24&quot;</span> Text=<span class="str">&quot;Column 9 From&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;Date&quot;</span>&gt;N/A&lt;/Property&gt;
&lt;/Properties&gt;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><u></u></p>
<p>&#160;</p>
<p><u>New Settings</u></p>
<pre class="csharpcode">&lt;Properties xmlns:xsi=<span class="str"><a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a></span> </pre>
<pre class="csharpcode">xmlns:xsd=<span class="str">&quot;http://www.w3.org/2001/XMLSchema&quot;</span>&gt;
    &lt;Property Name=<span class="str">&quot;PerformanceType1&quot;</span> ProID=<span class="str">&quot;1&quot;</span> Text=<span class="str">&quot;Gross&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;On|Off&quot;</span>&gt;On&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark1&quot;</span> ProID=<span class="str">&quot;3&quot;</span> Text=<span class="str">&quot;Benchmark 1&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;changed&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark2&quot;</span> ProID=<span class="str">&quot;4&quot;</span> Text=<span class="str">&quot;Benchmark 2&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Region1&quot;</span> ProID=<span class="str">&quot;9&quot;</span> Text=<span class="str">&quot;Changed&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|1 Month|2 Month|3&quot;</span>&gt;1 Month&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;NewData&quot;</span> ProID=<span class="str">&quot;29&quot;</span> Text=<span class="str">&quot;Column 9 From&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;Date&quot;</span>&gt;NewlyAdded&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;FromDate9&quot;</span> ProID=<span class="str">&quot;24&quot;</span> Text=<span class="str">&quot;Column 9 From&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;Date&quot;</span>&gt;N/A&lt;/Property&gt;
&lt;/Properties&gt;</pre>
<pre class="csharpcode">&#160;</pre>
<pre class="csharpcode"><u>Expected Output</u></pre>
<pre class="csharpcode"><u></u>&#160;</pre>
<pre class="csharpcode">&lt;Properties xmlns:xsi=<span class="str"><a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a></span></pre>
<pre class="csharpcode"><span class="str"></span> xmlns:xsd=<span class="str">&quot;http://www.w3.org/2001/XMLSchema&quot;</span>&gt;
    &lt;Property Name=<span class="str">&quot;PerformanceType1&quot;</span> ProID=<span class="str">&quot;1&quot;</span> Text=<span class="str">&quot;Gross&quot;</span> </pre>
<pre class="csharpcode">Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;On|Off&quot;</span>&gt;On&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark1&quot;</span> ProID=<span class="str">&quot;3&quot;</span> Text=<span class="str">&quot;Benchmark 1&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Benchmark2&quot;</span> ProID=<span class="str">&quot;4&quot;</span> Text=<span class="str">&quot;Benchmark 2&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|2nd Security Index&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;Region1&quot;</span> ProID=<span class="str">&quot;9&quot;</span> Text=<span class="str">&quot;Changed&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;ddlb&quot;</span> Values=<span class="str">&quot;N/A|1 Month|2 Month|3&quot;</span>&gt;1 Month&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;FromDate9&quot;</span> ProID=<span class="str">&quot;24&quot;</span> Text=<span class="str">&quot;Column 9 From&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;Date&quot;</span>&gt;N/A&lt;/Property&gt;
    &lt;Property Name=<span class="str">&quot;NewData&quot;</span> ProID=<span class="str">&quot;29&quot;</span> Text=<span class="str">&quot;Column 9 From&quot;</span></pre>
<pre class="csharpcode"><span class="str"></span> Type=<span class="str">&quot;Date&quot;</span>&gt;NewlyAdded&lt;/Property&gt;
&lt;/Properties&gt;</pre>
<pre class="csharpcode">&#160;</pre>
<pre class="csharpcode">&#160;</pre>
<pre class="csharpcode">&#160;</pre>
<p>And now Lets see how the code works:</p>
<pre class="csharpcode">&#160;</pre>
<pre class="csharpcode"><span class="kwrd">string</span> original = File.ReadAllText(<span class="str">@&quot;&lt;Original File path&gt;&quot;</span>);
<span class="kwrd">string</span> New = File.ReadAllText(<span class="str">@&quot;&lt;New File path&gt;&quot;</span>);
XmlCompare compare = <span class="kwrd">new</span> XmlCompare();
XmlDocument docs=compare.compare(original, New);
File.WriteAllText(<span class="str">@&quot;&lt;output path&gt;&quot;</span>, docs.OuterXml);</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<pre class="csharpcode">&#160;</pre>
<pre class="csharpcode">&#160;</pre>
<p>The Implementation of the code and its functionality is given in the below download file, with inline comments.</p>
<p>Please note few values are hard coded in this class, but can be altered to suit the requirements.</p>
<p><strong><a href="http://www.aneef.net/wp-content/uploads/2008/10/XMLCompare.cs" target="_blank">XMLCompare.cs</a></strong></p>
<p>I hope it helps for a kick start!.</p>
<p>&#160;</p>
<pre class="csharpcode">&#160;</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p><!--adsense#InPostImage--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.aneef.net/2008/10/30/compare-and-update-xml-file-with-the-changes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: www.aneef.net @ 2012-02-04 23:29:34 by W3 Total Cache -->
