I have a simple schema where I'm declaring both minOccurs and maxOccurs to 1. When I run the XSD.exe to generate a C# class and consume the class in code; the field is not enforced as mandatory. Is there some additional step missing? or does the classes generated using XSD.exe don't mandatory fields?
any suggestions or insight will be helpful.
The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.
The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once.
It is primarily used to define the elements, attributes and data types the document can contain. The information in the XSD is used to verify if each element, attribute or data type in the document matches its description.
xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.
Like the Xml / infer schema tool in visual studio, whenever I've used XSD.exe I've ended up fixing some of the generated code. XSD.exe does a good approximation but it doesn't work entirely.
The minOccurs / maxOccurs 'bug' is documented on MSDN.
When generating source code from an XML Schema document, Xsd.exe ignores the minOccurs attribute applied to the < choice >, < sequence >, < group >, < all >, and < any > elements.
For the element, Xsd.exe ignores the minOccurs attribute if the value of the maxOccurs attribute is greater than 1, or unbounded. In this case, the tool produces an array of the type corresponding to the XSD data type. Xsd.exe uses the value of the maxOccurs attribute to determine whether to produce a single instance or an array.
For the element, Xsd.exe also ignores the minOccurs attribute if it is applied to a schema data type that converts to a .NET Framework reference type.
Only when all the following conditions are true does Xsd.exe utilize the value of the minOccurs attribute:
The element is involved.
The maxOccurs attribute dictates a single instance.
The data type converts to a value type.
How would you enforce an arbitrary minoccurs in code? Some sort of assertions? I guess you can only map to "Non-nullable single instance" (1:1) non-nullable array (1:many) and their nullable equivalents (0;1), and (0:many)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With