XMLUnit provides you with the tools to verify the XML you emit is the one you want to create. It provides helpers to validate against an XML Schema, assert the values of XPath queries or compare XML documents against expected outcomes.
$ dotnet add package XMLUnit.CoreXMLUnit provides you with the tools to verify the XML you emit is the one you want to create.
It provides helpers to validate against an XML Schema, assert the values of XPath queries or compare XML documents against expected outcomes.
This package provides the core functionality and can be used stand-alone. In addition there are libraries providing NUnit constraints and a "placeholders" package that may simplify writing comparison tests in certain cases.
[]
XMLUnit requires .NET Standard 2.0 (tested with .NET 8 rigt now) and should still support .NET Framework 3.5 and Mono.
The core library hasn't got any dependencies itself.
These are some really small examples, more is available as part of the user guide
ISource control = Input.FromFile("test-data/good.xml").Build();
ISource test = Input.FromByteArray(CreateTestDocument()).Build();
IDifferenceEngine diff = new DOMDifferenceEngine();
diff.DifferenceListener += (comparison, outcome) => {
Assert.Fail("found a difference: {}", comparison);
};
diff.Compare(control, test);
or using the fluent builder API
Diff d = DiffBuilder.Compare(Input.FromFile("test-data/good.xml"))
.WithTest(CreateTestDocument()).Build();
Assert.IsFalse(d.HasDifferences());
ISource source = Input.FromString("<foo>bar</foo>").Build();
IXPathEngine xpath = new XPathEngine();
IEnumerable<XmlNode> allMatches = xpath.SelectNodes("/foo", source);
string content = xpath.evaluate("/foo/text()", source);
Validator v = Validator.ForLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
v.SchemaSources = new ISource[] {
Input.FromUri("http://example.com/some.xsd").Build(),
Input.FromFile("local.xsd").Build()
};
ValidationResult result = v.ValidateInstance(Input.FromDocument(CreateDocument()).Build());
bool valid = result.Valid;
IEnumerable<ValidationProblem> problems = result.Problems;
XMLUnit.NET is developed at github. More documentation, releases and an issue tracker can be found there.
See the Release Notes at github.