Skip to main content

Matching XML

Describes the possible expectations for matching an XElement or an XDocument with an expected XML, and the options to relax the comparison.

ExpectationSummary
Matchesmatches the expected XML, additional nodes allowed
MatchesExactlymatches the expected XML exactly

Elements and documents​

You can verify that an XElement matches an expected XML:

XElement track = XElement.Parse(
"""<track title="Let It Be" artist="The Beatles"><genre>rock</genre></track>""");

await That(track).Matches("""<track title="Let It Be" />""");
await That(track).MatchesExactly("""
<track artist="The Beatles" title="Let It Be">
<genre>rock</genre>
</track>
""");

Matches ignores the attributes and child nodes of the subject that are not in the expected XML, while MatchesExactly fails for them: Matches is MatchesExactly with the option IgnoringAdditionalNodes(). Missing nodes, different values and a different order of the expected child elements fail both, unless you ignore the element order. If the title was "Yesterday" instead, Matches would fail with:

Failure message
Expected that track
matches <track title="Let It Be" />,
but it differed as /track/@title was "Yesterday" instead of "Let It Be"

XML options:
- ignore additional nodes

The elements are compared by their content like by AsXml(): attributes regardless of their order, elements and attributes by their namespace and local name instead of their prefix, and without comments and processing instructions. Whitespace-only text between elements is ignored.

For an XDocument, its root element is compared:

XDocument document = XDocument.Parse("""<?xml version="1.0"?><track title="Let It Be" />""");

await That(document).Matches("""<track title="Let It Be" />""");

You can also verify that an element does not match an expected XML:

XElement track = XElement.Parse("""<track title="Let It Be" />""");

await That(track).DoesNotComplyWith(it => it.Matches("""<track title="Yesterday" />"""));
Whitespace, paths and documents without a root element

Whitespace-only text between elements is ignored also when the subject was loaded with LoadOptions.PreserveWhitespace, unless xml:space="preserve" on the subject or one of its ancestors keeps it. The paths of the differences start at the subject, also when it is nested in a document. A document without a root element fails, also when the expectation is negated.

Options​

The options are available wherever XmlOptions are accepted, i.e. Matches, MatchesExactly, IsValidXmlMatching, IsValidXmlMatchingExactly and AsXml(o => …). They work like the options of aweXpect:

OptionConfigures
IgnoringAdditionalNodes()ignores additional attributes and child nodes of the subject
IgnoringMember(path), IgnoringMembers(path => …)leaves members out of the comparison
IgnoringElementOrder()the child elements and texts can be in any order

Additional nodes​

You can ignore the attributes and child nodes of the subject that are not in the expected XML. The option is available for MatchesExactly, IsValidXmlMatchingExactly and AsXml(), and Matches and IsValidXmlMatching use it by default:

string album = """<album id="42" year="1969"><track title="Come Together" /></album>""";

await That(album).IsEqualTo("""<album id="42" />""").AsXml(o => o.IgnoringAdditionalNodes());

The additional nodes are ignored on every level, and the expected nodes are still compared, so that missing nodes, different values and a different order of the expected child elements fail the expectation. The option is listed when the expectation fails:

Failure message
XML options:
- ignore additional nodes

With IgnoringAdditionalNodes(false), Matches and IsValidXmlMatching compare exactly, like MatchesExactly and IsValidXmlMatchingExactly.

Ignoring members​

You can ignore members whose values change between runs, e.g. generated ids or timestamps. The options are available for Matches, MatchesExactly, IsValidXmlMatching, IsValidXmlMatchingExactly and AsXml():

string album = """<album id="42" createdAt="2026-10-17"><track title="Come Together" etag="x1" /></album>""";

await That(album).IsEqualTo("""<album id="1"><track title="Come Together" /></album>""")
.AsXml(o => o
.IgnoringMember("/album/@id")
.IgnoringMember("@createdAt")
.IgnoringMembers(path => path.EndsWith("/@etag")));

IgnoringMember matches whole trailing segments of the path: "@createdAt" ignores the attribute on every element and "track/@id" the attribute of every track, while a path starting with /, e.g. "/album/@id", only ignores this member. A segment without a position, e.g. track, matches every element of the name, and track[2] only the second. The names are compared case-sensitively, as in XML. Like the paths, they are local names, so that "track" ignores a track in every namespace, unlike HasElement, which looks up the name with its namespace. IgnoringMembers receives the path of every compared member instead.

An ignored attribute, element or text, and everything below it, is neither compared nor reported as missing or unexpected, and an ignored element is not considered for the order of its siblings. The ignored members are listed when the expectation fails:

Failure message
XML options:
- ignore members: ["/album/@id", "@createdAt", path => path.EndsWith("/@etag")]

A member to ignore that matched no compared path is named in a failure, with the path it most likely meant, as a misspelled member to ignore would otherwise ignore nothing without notice:

Failure message
XML options:
- ignore members: ["@createdat"]
"@createdat" matched no member (did you mean "@createdAt"?)
Members that only occur in the subject

As additional nodes of the subject are not compared when they are ignored, e.g. by Matches, a member to ignore that only occurs in the subject matches no compared path either, and is named as well, although it is not needed.

Element order​

By default, the order of the child elements and texts is significant. You can ignore it for all compared elements:

XElement album = XElement.Parse("""
<album>
<track title="Something" />
<track title="Come Together" />
</album>
""");

await That(album).MatchesExactly("""
<album>
<track title="Come Together" />
<track title="Something" />
</album>
""",
o => o.IgnoringElementOrder());

The texts are paired like the elements, so that the order of the texts in mixed content, e.g. <p>a<b/>c</p>, is ignored as well.

How the children are paired

Each expected child is paired with the first unpaired child of the subject with the same name that matches it. The remaining children of the same name are paired in their order, so that their differences are reported, e.g. /album/track[1]/@title was "Here Comes the Sun" instead of "Come Together", and the children that are still unpaired are reported as missing or unexpected. This greedy pairing can fail when additional nodes are ignored, e.g. by Matches, and an expected child matches several children of the subject, even if another pairing would succeed.

As the children are no longer paired by their position, a position in a member to ignore, e.g. track[2], refers to the child at this position in each document.