Check for extra fields in XML with BizUnit

With BizUnit it is easy to create checks on the values of XML nodes. It is also easy to add a count for specific repeating elements. However, it is difficult to check if there are unexpected nodes or attributes in the message, without creating counts for all nodes/attributes in the message.

Today I came up with an easy way to verify if no nodes or attributes are added (or removed, for that matter).

The xpath descendant::node() will flatten your XML (both elements, values and attributes). Add a count around this and you are set to verify if any unexpected nodes or attributes are in the XML. Below is what your XPathValidation node would look like – just add it at the bottom of your ValidationStep:

<!-- Check for unexpected nodes in the XML -->
<XPathValidation query=”count(descendant::node())”>123</XPathValidation>

You could get the count with DanSharp XML Viewer or by running the test once and checking the found value.