1

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?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
maurocam
  • 411
  • 6
  • 15

2 Answers2

0
<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">   
<xs:element name="TestResults" >
<xs:complexType>
<xs:sequence>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="TestResult" type="xs:string"/>
        <xs:element name="atoken" type="xs:string"/>
    </xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Please have a look at XSD - how to allow elements in any order any number of times?

Community
  • 1
  • 1
Kanagavelu Sugumar
  • 18,766
  • 20
  • 94
  • 101
  • Thanks but I am afraid neither of the 2 options work. In Option 1 , I get an error saying 'maxOccurs' on the root must be 1. If I change to it then goes back to my original error for 'TestResult'. For Option 2 I get an error saying that maxOccurs in an 'all' group must be 0 or 1. – maurocam Feb 12 '13 at 18:04
  • I am afraid the alternative you suggest also does not work. I get an error which seems to indicate that 'TestResults' as the root element cannot have nested sequences: Error: Type 'TestResults' in namespace '' cannot be imported. The root sequence must contain only local elements. Group ref, choice, any and nested sequences are not supported. Either change the schema so that the types can map to data contract types or use ImportXmlType or use a different serializer. – maurocam Feb 13 '13 at 09:51
  • @maurocam Coul you please post you entire schema here? – Kanagavelu Sugumar Feb 13 '13 at 10:03
  • Sorry cannot post entire schema, as it is confidential. Would need time to render it generic. The key point is that 'TestResult' is a typed collection of 'TestResultType', and not an 'xs:string' as you indicate. – maurocam Feb 13 '13 at 10:41
  • @maurocam I think; if you have tried the above all the updates; if still not working then the issue is not with XSD. Need to check with parser and XML. Finally i added string to validate the XSD from my side, while testing you can change it to yours. Bye! – Kanagavelu Sugumar Feb 13 '13 at 11:27
  • Thanks for your help. The suggestion of nesting sequences is what led me to the solution. – maurocam Feb 13 '13 at 14:05
  • @maurocam if time permits Could you please try in update_2 – Kanagavelu Sugumar Feb 13 '13 at 15:29
0

Thanks for suggestions on how to fix the problem.

After various attempts, below is how I finally got it to work. I have had to add an intermediate element which holds my 'TestResult' collection. In this way my outer sequence holds two elements that are both 'simple' elements (though they are not really)

<xs:element name="TestResults">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Token" type="IdentifierType" minOccurs="1" maxOccurs="1"/>
      <xs:element name="TestResultList">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="TestResult" type="TestResultType" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
maurocam
  • 411
  • 6
  • 15