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
(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 );
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 = true
option 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
Well, that didn't do antything
– Shmosi
Sep 17 '18 at 6:19