XSD.exe doesn't enforce minOccurs

北城余情 提交于 2019-12-08 17:51:43

问题


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.


回答1:


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.




回答2:


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)




回答3:


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



来源:https://stackoverflow.com/questions/2758549/xsd-exe-doesnt-enforce-minoccurs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!