Compare XML file (attributes, nodes and text)

Compare XML file (attributes, nodes and text)



I know that there are already several questions about this topic, but none of the solutions seem to work for me. I already tried everything with Diff in the name, XMLUnit, FatAntelope, xdocument and several more.
My main problem with These approaches was, that I either don't get the output I want or no output at all. The best approach was XmlDiffLib with the following code:


var exampleA = File.ReadAllText(@"Error0.xml");
var exampleB = File.ReadAllText(@"Error1.xml");

var diff = new XmlDiff(exampleA, exampleB);

diff.CompareDocuments(new XmlDiffOptions());

Console.WriteLine(diff.ToString());
Console.ReadLine();



but it only shows me changed text (f.e. <error>TextThatCanBeDetected</error>). I want additional to check for changed attributes (<error same="IWantToCheckThisToo"/>)


<error>TextThatCanBeDetected</error>


<error same="IWantToCheckThisToo"/>



Inline Edit
diff has other flaws too, for example if in the new XML is an extra tag it won't be recognized as one
End inline edit



I would be very happy to help me with this problem, below is an example of the XML file


<?xml version="1.0" encoding="UTF-8"?>
<message>
<header>
</header>
<body>
<error name="MyName"> MyInnerXML </error>
</body>
</message>



(Note that the original file is around 100 lines long)



EDIT



So one answer was to change the CompareDocuments method to


diff.CompareDocuments(new XmlDiffOptions IgnoreAttributes=false );



but the output was this



(I used wudri and hudri for the name tag, it tells me that wudri is not found but nothing about hudri)




1 Answer
1



Simply specify to not ignore attributes in your XmlDiffOptions:


diff.CompareDocuments(new XmlDiffOptions IgnoreAttributes = false );



Moreover you need to specify to compare both directions to identify both values for the differing node:


diff.CompareDocuments(new XmlDiffOptions IgnoreAttributes = false, TwoWayMatch = true );






Well, that didn't do antything

– Shmosi
Sep 17 '18 at 6:19






I tested with your sample Xml and it clearly identified the difference between name="MyName1"and name="MyName2".

– Filburt
Sep 17 '18 at 6:22


name="MyName1"


name="MyName2"






I edited my answer to the Output I am getting

– Shmosi
Sep 17 '18 at 10:10






Adding the TwoWayMatch = trueoption should give you the desired result, identifying both values for the differing node.

– Filburt
Sep 17 '18 at 10:52


TwoWayMatch = true






Well basically it works. The output is quite hard to read but the desired output is there. Problem: it doesn't work with my 100+ lines like mentioned in my question. Even the developer of XmlDiffLib states this as a problem with his code on github

– Shmosi
Sep 17 '18 at 10:57




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế