views:

224

answers:

3

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.

A: 

You are correct that XSD.EXE does not enforce occurrence requirements. This is as designed.

John Saunders
+2  A: 

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.

amelvin
A: 

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)

Doobi