Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you compare two XML Documents?

As part of the base class for some extensive unit testing, I am writing a helper function which recursively compares the nodes of one XmlDocument object to another in C# (.NET). Some requirements of this:

  • The first document is the source, e.g. what I want the XML document to look like. Thus the second is the one I want to find differences in and it must not contain extra nodes not in the first document.
  • Must throw an exception when too many significant differences are found, and it should be easily understood by a human glancing at the description.
  • Child element order is important, attributes can be in any order.
  • Some attributes are ignorable; specifically xsi:schemaLocation and xmlns:xsi, though I would like to be able to pass in which ones are.
  • Prefixes for namespaces must match in both attributes and elements.
  • Whitespace between elements is irrelevant.
  • Elements will either have child elements or InnerText, but not both.

While I'm scrapping something together: has anyone written such code and would it be possible to share it here?

On an aside, what would you call the first and second documents? I've been referring to them as "source" and "target", but it feels wrong since the source is what I want the target to look like, else I throw an exception.

like image 590
Neil C. Obremski Avatar asked Oct 03 '08 17:10

Neil C. Obremski


People also ask

How do I compare two XML files in Visual Studio?

Steps to compare contents of two files Open both the files in VS Code. 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.

How do I compare two XML files in Postman?

Using the function difference() , you can test if the 2 xmls have any differences.


1 Answers

Microsoft has an XML diff API that you can use.

Unofficial NuGet: https://www.nuget.org/packages/XMLDiffPatch.

like image 143
Danimal Avatar answered Sep 30 '22 19:09

Danimal