Reputation: 5384
Imagine one XML as:
<foo>
<node1>Some value</node1>
<node2>BB</node2>
<node3>TTTTT</node3>
<node4>XXXX</node4>
</foo>
and another XML as:
<foo>
<node1>Something Else</node1>
<node4>XXXX</node4>
<node5>TTTTT</node5>
</foo>
The difference count here is 3
a) node1 value is different
b) node2 is missing in 2nd XML
c) node5 is missing in 1st XML
I've tried using the XMLDiff class but the result is too cumbersome for my needs.
Schema:
Root named "foo" and one single set of childrens with one value each.
Question:
What's the simplest and fastest way to code this in C#?
Upvotes: 1
Views: 76
Reputation: 163458
One way to do this might be to generate a list of XPath assertions from your first document, in the form:
/foo/node1 = "Some value"
/foo/node2 = "BB"
/foo/node3 = "TTTT"
/foo/node4 = "XXXX"
and then apply these assertions to the second document to count how many of them are true. Because this won't catch data that is absent on the first document and present in the second, you might want to do the inverse as well. It's not perfect, of course, for example it won't catch differences in element order. But you haven't actually defined what you mean by a significant difference, and you could adjust the XPath expressions to assert what you consider significant. For example you could vary the last assertion to:
count(/foo/node4[. = "XXXX"]) = 1
The simplest and fastest way to code this, of course, is not in C#, unless that happens to be the only programming language you know. Using XSLT or XQuery would be much better.
Upvotes: 1
Reputation: 270
Have you considered using XNode.DeepEquals, with the root (in this case 'foo') of each XML file being your node? The MSDN page on how to use it is here:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.deepequals.aspx
Upvotes: 1