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.
Here are few suggestion I got from my friends and communities:
- XML Diff Patch - Couldn’t achieve what I wanted.
- Linq for XML - I cannot use this as the application is developed under .net Framework 2.0
So after Several Discussions and Forum Posts I Decided to Write one on my own. I don’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 it needs some improvements such as comparing nested nodes and attributes. but this would be a better kick start. "something is better than nothing" right!!!.
Here are two sample XML files which we can use for our comparison, and the output expected.
Existing Settings
<Properties xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Property Name="PerformanceType1" ProID="1" Text="Gross"
Type="ddlb" Values="On|Off">On</Property> <Property Name="Benchmark1" ProID="3" Text="Benchmark 1" Type="ddlb"
Values="N/A|2nd Security Index">N/A</Property> <Property Name="Benchmark2" ProID="4" Text="Benchmark 2" Type="ddlb"
Values="N/A|2nd Security Index">N/A</Property> <Property Name="Region1" ProID="7" Text="Column 1" Type="ddlb"
Values="N/A|1 Month|2 Month|3">1 Month</Property> <Property Name="FromDate1" ProID="8" Text="Column 1 From"
Type="Date">N/A</Property> <Property Name="FromDate9" ProID="24" Text="Column 9 From"
Type="Date">N/A</Property>
</Properties>
New Settings
<Properties xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Property Name="PerformanceType1" ProID="1" Text="Gross"
Type="ddlb" Values="On|Off">On</Property> <Property Name="Benchmark1" ProID="3" Text="Benchmark 1"
Type="ddlb" Values="N/A|2nd Security Index">changed</Property> <Property Name="Benchmark2" ProID="4" Text="Benchmark 2"
Type="ddlb" Values="N/A|2nd Security Index">N/A</Property> <Property Name="Region1" ProID="9" Text="Changed"
Type="ddlb" Values="N/A|1 Month|2 Month|3">1 Month</Property> <Property Name="NewData" ProID="29" Text="Column 9 From"
Type="Date">NewlyAdded</Property> <Property Name="FromDate9" ProID="24" Text="Column 9 From"
Type="Date">N/A</Property>
</Properties>
Expected Output
<Properties xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Property Name="PerformanceType1" ProID="1" Text="Gross"
Type="ddlb" Values="On|Off">On</Property> <Property Name="Benchmark1" ProID="3" Text="Benchmark 1"
Type="ddlb" Values="N/A|2nd Security Index">N/A</Property> <Property Name="Benchmark2" ProID="4" Text="Benchmark 2"
Type="ddlb" Values="N/A|2nd Security Index">N/A</Property> <Property Name="Region1" ProID="9" Text="Changed"
Type="ddlb" Values="N/A|1 Month|2 Month|3">1 Month</Property> <Property Name="FromDate9" ProID="24" Text="Column 9 From"
Type="Date">N/A</Property> <Property Name="NewData" ProID="29" Text="Column 9 From"
Type="Date">NewlyAdded</Property>
</Properties>
And now Lets see how the code works:
string original = File.ReadAllText(@"<Original File path>"); string New = File.ReadAllText(@"<New File path>"); XmlCompare compare = new XmlCompare(); XmlDocument docs=compare.compare(original, New); File.WriteAllText(@"<output path>", docs.OuterXml);
The Implementation of the code and its functionality is given in the below download file, with inline comments.
Please note few values are hard coded in this class, but can be altered to suit the requirements.
I hope it helps for a kick start!.



Leave a Reply