Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two XmlNodes in C#

Tags:

c#

.net

xml

xmlnode

Is there a simple way to compare two XML structures to determine if they have the same structure and data?

I have a function that returns an XmlNode and I am trying to write unit tests for it. I store the correct XML result in a file. Durring the test I load the file into an XmlDocument, locate the proper XmlNode and compare against the result of the function. A straight compare does not work (as expected) and InnerXml does not work either. I am considering removing all whitespace from InnerXml and comparing that, or writing my own compare to walk the tree, but I don't like either option much.

like image 659
oillio Avatar asked Aug 21 '09 19:08

oillio


People also ask

Which of the following methods is used to compare two XML files?

The XMLUnit library can be used to compare two XML files in Java. Similar to JUnit, XMLUnit can also be used to test XML files for comparison by extending the XMLTestcase class. It is a rich library and provides a detailed comparison of XML files.

How do I compare two XML files in Visual Studio?

From the left Explorer panel, right-click the first file and choose Select for Compare from the right-click menu. Then right-click the second file and choose Compare with Selected. Both the files will be opened in the main panel, side by side in inline view mode which is comfortable for comparing the differences.


1 Answers

XNode.DeepEquals. Read the caveats before using it.

like image 125
Srikar Doddi Avatar answered Sep 28 '22 02:09

Srikar Doddi