The default for minOccurs
property of an <element>
in an XSD file is 1 (source).
Consider you have an XSD specifying a complex type containing 2 elements: <el1>
and <el2>
.
If you provide an XDocument
only containing <el2>
, the XDocument
will not validate.
Instead you'll get the message:
The element Message in namespace ___ has invalid child element el2 in namespace ___. List of possible elements expected: el1
This basically is an error on <el2>
where one would expect an error on the complex type for not containing <el1>
.
My question is:
Is there a way to check if all <element>
-tags which have minOccurs
> 0 are present?
I would say this is a very basic check when validating XML with an XSD.
Depending on the way you defined your schema, the order of appearance of elements will matter.
In this case the validator is expecting a <el1> but is seeing the element <el2> so the error is that <el2> is appearing where it should not. I belive that means you used a "sequence" when defining your complex type. So the error you at getting is correct.
If this still bothers you, and the order of the elements does not matter to your parsing use "all" instead of "sequence" which will not enforce order. The validator should then prompt you that a required element <el1> is missing. It should look something like the following:
<xsd:complexType name="MyType">
<xsd:all>
<xsd:element name="el1" minOccurs="1"/>
<xsd:element name="el2" minOccurs="1"/>
</xsd:all>
</xsd:complexType>
I hope this helps.