Reputation: 132
How can I configure XmlUnit.Net to ignore the XML declaration when comparing two documents?
Assume I have the following control document:
<?xml version="1.0" encoding="utf-8"?>
<a><amount>1</amount></a>
Which I want to compare with:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a><amount>1</amount></a>
The comparison should result in no differences.
My expectation would be that using a NodeFilter like so should work, but it doesn't:
var diff = DiffBuilder.Compare(control)
.WithTest(test)
.WithNodeFilter(n => n.NodeType != XmlNodeType.XmlDeclaration)
.Build();
diff.Differences.Count().Should().Be(0);
The assertion fails with two differences - one for the encoding (different casing) and another for the standalone attribute. I'm not interested in any.
Whether I say n.NodeType != XmlNodeType.XmlDeclaration
or n.NodeType == XmlNodeType.XmlDeclaration
makes no difference.
I am using XMLUnit.Core v2.5.1.
Upvotes: 5
Views: 1658
Reputation:
NodeFilter
only applies to nodes that are children of other nodes (returned by XmlNode.ChildNodes
). Unfortunately this is not the case for the document type declaration, which probably is a bug.
In your case you want to tweak the DifferenceEvaluator
and downgrade the differences you are not interested in. Something like
DifferenceEvaluators.Chain(DifferenceEvaluators.Default,
DifferenceEvaluators.DowngradeDifferencesToEqual(ComparisonType.XML_STANDALONE, ComparisonType.XML_ENCODING))
would swallow the differences.
Maybe you don't want to just count the differences but also look at their severity. The difference in encoding would be a "similar" difference, while the different values of standalone are critical.
Upvotes: 6