I am encountering a very strange error when using Svcutil to convert XSD to C# objects.
Here is my XSD
<xs:element name="TestResults">
<xs:complexType>
<xs:sequence>
<xs:element name="TestResult" type="TestResultType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="atoken" type="IdentifierType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
When I run Svcutil the error I get is
D:\CambridgeAssessment\Documents\CA\BeaconSchemas-20130211>svcutil /dconly testResults.1.0.xsd
Error: Type 'TestResults' in namespace 'http://ucles/schema/ukba/TestResults/1/0
' cannot be imported. 'maxOccurs' on element 'TestResult' must be 1. Either chan
ge the schema so that the types can map to data contract types or use ImportXmlT
ype or use a different serializer.
If you are using the /dataContractOnly option to import data contract types and
are getting this error message, consider using xsd.exe instead. Types generated
by xsd.exe may be used in the Windows Communication Foundation after applying th
e XmlSerializerFormatAttribute attribute on your service contract. Alternatively
, consider using the /importXmlTypes option to import these types as XML types t
o use with DataContractFormatAttribute attribute on your service contract
If I set 'TestResult' attribute 'maxOccurs' = 1, then it all works fine. It also works with 'TestResult' attribute 'maxOccurs' = 'unbounded' if I completely remove the 'atoken' element.
Looking at the schema refernce for DataContractSerializer, I found the following:
<xs:element> can occur in the following contexts:
It can occur within an <xs:sequence>, which describes a data member of a regular (non-collection) data contract. In this case, the maxOccurs attribute must be 1. (A value of 0 is not allowed).
It can occur within an <xs:sequence>, which describes a data member of a collection data contract. In this case, the maxOccurs attribute must be greater than 1 or "unbounded".
So, it looks like in my particular XSD, Svcutil thinks that BOTH elements should have 'maxOccurs'=1, even the one which is a collection.
Is this behaviour correct? Or am I doing something wrong?