Technically, XMLs are different

  • if they have whitespaces or not
  • if the order is different
  • if they have comments or not
  • if they have processing instructions or not
  • if their encoding is different
  • if their namespaces are different

but of course you can decide to ignore that or not, based on the semantic information an XML does not have.

Microsoft has developed the XML Diff and Patch tool for this purpose and you can integrate it in your own applications.

Note: the tool installs as "SQLXML Bulkload in .NET Code Sample" and comes with a Visual Studio solution XmlDiffView.sln that you need to compile yourself. Some basic programming knowledge in C# and Visual Studio Community Edition should be ok.

However, as mentioned in one of the answers on Stack Overflow, it has been compiled and made available on Bitbucket.

After that it comes with a UI that let's you choose the various XML comparison options:

When I apply it to the 2 XMLs of your questions, it throws an exception. That is because of the namespaces which are not defined. After removing the namespaces, it says:

Answer from Thomas W. on Stack Exchange
Top answer
1 of 6
9

Technically, XMLs are different

  • if they have whitespaces or not
  • if the order is different
  • if they have comments or not
  • if they have processing instructions or not
  • if their encoding is different
  • if their namespaces are different

but of course you can decide to ignore that or not, based on the semantic information an XML does not have.

Microsoft has developed the XML Diff and Patch tool for this purpose and you can integrate it in your own applications.

Note: the tool installs as "SQLXML Bulkload in .NET Code Sample" and comes with a Visual Studio solution XmlDiffView.sln that you need to compile yourself. Some basic programming knowledge in C# and Visual Studio Community Edition should be ok.

However, as mentioned in one of the answers on Stack Overflow, it has been compiled and made available on Bitbucket.

After that it comes with a UI that let's you choose the various XML comparison options:

When I apply it to the 2 XMLs of your questions, it throws an exception. That is because of the namespaces which are not defined. After removing the namespaces, it says:

2 of 6
6

Focusing on the part that moved sections should be reported as no difference made me think of https://semanticmerge.com/, which doesn't compare XML-files, but C# and C code. And as it understand those languages it is able to display if code has moved and not changed.

This leads to an alternative approach for this question: Could it be possible to translate the XML into C# classes, and then do a semantic merge on the resulting code?

One possible approach, if this tool is not written already, could be to translate each and every element to classes, and each attribute (and body texts) to a string property within that class. If you want to ignore namespaces, then let your translator remove them in the translation process.

I translated the XML example given as proof of concept and got the following:

class soapenv__Body {
  class mes__GetItem {
    class mes__ItemShape {
      class typ__BaseShape {
          string body="IdOnly";
      }
      class typ__BodyType {
          string body="Textus";
      }
      class typ__AdditionalProperties {
        class typ__FieldURI  {
            string FieldURI="item:Subject";
        }
        class typ__FieldURI  {
            string FieldURI="item:Categories"; 
        }
      }
    }
    class mes__ItemIds {
      class typ__ItemId {
          string Id="AAMYAAA=";
      }
    }
  }
}

Then I switched the mes:ItemIds and mes:ItemShape and changed the text to Textus. Compared the following two files in Semantic Merge and got the following image:

In this image one can see the move, indicated by the M icon, and the change in text indicated by the C icon. Lines indicates where the different parts have moved/changed, and it possible to actually see the differences if they exist.

Note that Semantic Merge even though understanding C# code, isn't to strict on the identical class names of typ__FieldURI, which could be a nice features as XML can contain multiple nodes with the same name.

Summa summarum: Semantic Merge can correctly identify the XML as identical (or not) even though elements move, if you can convert the XML into a C# class structure.

Top answer
1 of 11
36

Have a look at File comparison tools, from which I am using WinMerge. It has an ability to compare XML documents (you may wish to enable DisplayXMLFiles prefilter for v2.14.0 or PrettifyXML for v2.16.x).

DisplayXMLFiles.dll - This plugin pretty-prints XML files nicely by inserting tabs and line breaks. This is useful for XML files that do not have line returns in convenient locations.

Note for v2.16.x

The plugin PrettifyXML is supplied with the software package. It can be activated as following:

  • Make sure that Plugins โ†’ Plugin Settings โ†’ Enable plugins is enabled.
  • Then use either File โ†’ Recompare As โ†’ Prettify XML or Plugins โ†’ Scripts โ†’ Prettify XML

For those who wish to play with deprecated DisplayXMLFiles, he needs to download e.g. winmerge-2.16.14-full-src.7z and copy Plugins\dlls\X64\DisplayXMLFiles.dll to WinMerge\MergePlugins\.

See also my feature comparison table.

2 of 11
22

I wrote and released a Windows application that specifically solves the problem of comparing and merging XML files.

Project: Merge can perform two and three way comparisons and merges of any XML file (where two of the files are considered to be independent revisions of a common base file). You can instruct it to identify elements within the input files by attribute values, or the content of child elements, among other things.

It is fully controllable via the command line and can also generate text reports containing the differences between the files.

๐ŸŒ
SourceForge
diffxml.sourceforge.net
diffxml - XML Diff and Patch Utilities
diffxml & patchxml: Tools for comparing and patching XML files ยท The standard Unix tools diff and patch are used to find the differences between text files and to apply the differences. These tools operate on a line by line basis using well-studied methods for computing the longest common ...
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ windows-developer-power โ€บ 0596527543 โ€บ ch19s07.html
19.6 Comparing and Unit Testing XML with XML Diff and Patch - Windows Developer Power Tools [Book]
The included samples are the source for the command-line utilities and a sample to create an HTML representation of the difference between two XML documents. In addition to comparing full XML documents, the tools can compare and patch XML fragments.
๐ŸŒ
GitHub
github.com โ€บ Shoobx โ€บ xmldiff
GitHub - Shoobx/xmldiff: A library and command line utility for diffing xml
There is also a method diff_trees() that take two lxml trees, and a method diff_texts() that will take strings containing XML.
Starred by 226 users
Forked by 53 users
Languages ย  Python 98.7% | Python 98.7%
๐ŸŒ
TextCompare
textcompare.org โ€บ xml
Online Xml Compare Tool
It works on any modern browser like Firefox, Chrome, Edge etc on Windows, Mac or Linux based computer. Unlimited: There is no limit on number of comparisons you can do. You can compare as many Xml files as you want. No, all the processing is done on your ๐Ÿ–ฅ browser, so nothing is saved on our server unless you choose to save to share online. For private sharing, you can download the diff as PDF file.
Find elsewhere
๐ŸŒ
7-Zip Documentation
documentation.help โ€บ Microsoft-XML-Diff โ€บ xmldiffpatch_xmldiff_ref_1bqb.htm
XmlDiff Class - Microsoft XML Diff Documentation
Microsoft XML Diff Documentation ... Represents the class that performs a comparison of XML documents, fragments, or nodes. For a list of all members of this type, see XmlDiff Members. ... Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP, Windows .NET ...
๐ŸŒ
Xlcompare
xlcompare.com โ€บ compare-xml-files.html
Compare XML Files Online. Free. No Upload. Get the Semantic XML diffs.
October 3, 2009 - Need to find a specific element in the XML file? Just press CTRL+F and enter the search string. XML Compare will do this for you! Replace names of specific nodes by calling the replace window with the CTRL+H hotkey and entering the pattern and ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ archive โ€บ blogs โ€บ dmahugh โ€บ open-xml-diff-utility
Open XML diff utility | Microsoft Learn
Stephane Rodriguez has decided to work through those details, and the result is a very useful utility for Open XML developers and users: DIFFOPC, a diff tool for OPC (Open Packaging Convention) packages such as Open XML documents and XPS (XML Paper Specification) documents. You can download Stephane's diff tool here, and he also has provided some information about how to use it. I've tried this tool on Windows Server 2003, XP, and Vista, and it works great.
๐ŸŒ
SmartBear
smartbear.com โ€บ blog โ€บ how-to-compare-xml-files-using-diffxml
How to Compare XML Files Using Diffxml
Open a command line window and cd to your diffxml directory. Type diffxml.bat and press Enter to diff the demonstration files. If you donโ€™t see an error then the output from diffxml should be in out1.xml.
๐ŸŒ
Readthedocs
xmldiff.readthedocs.io โ€บ en โ€บ stable โ€บ commandline.html
Command-line Usage โ€” xmldiff documentation
To use it from the command-line, just run xmldiff with two input files: ... There are a few extra options to modify the output, but be aware that not all of the combinations are meaningful, so donโ€™t be surprised of you add one and nothing happens. You can select different output formats with xmldiff, but beware that some formatters may assume certain things about the type of XML.
๐ŸŒ
Advanced Installer
advancedinstaller.com โ€บ user-guide โ€บ xml-diff.html
External Diff for XML files
Select an XML file from Files And Folders page. From the context menu select the โ€œExternal Diff Withโ€ item
๐ŸŒ
Altova
altova.com โ€บ diffdog
DiffDog Diff/Merge Tool | Altova
You can save the SQL change script to a file, send the script to a SQL Editor window in DatabaseSpy, execute the SQL change script directly from DiffDog, or cancel your merge request and abandon the script. Altova DiffDog includes enhanced support to diff/merge Microsoft Office Open XML (OOXML) files.
๐ŸŒ
SemanticDiff
semanticdiff.com โ€บ online-diff โ€บ xml
SemanticDiff - Compare XML Online
Compare two XML files in your browser using our free semantic diff tool. See how values and attributes differ while ignoring formatting changes.