Collection<T> in xsd Schema?

Jan 26, 2009 at 12:44 PM
Hi,

I have gone through the hand on labs and everything worked fine.
However, if I use my own business entities and xsd schema for the response messages, I am not able to use the DataContractSerializer to generate the code. I'm always getting the same message:

The file name 'myschema.xsd' is not compliant with the DataContractSerializer. Type 'Mytype' in namespace 'http://someuri/commontypes' 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.

My Schema for the response message is looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://someuri/commontypes" xmlns:mstns="http://someuri/commontypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://someuri/commontypes" elementFormDefault="qualified">
    <xs:element name="MytypeElement" type="Mytype"/>
    <xs:complexType name="Mytype">
        <xs:sequence>
            <xs:element name="PropertyA" type="xs:double" nillable="false"/>
            <xs:element name="PropertyB" type="xs:double" nillable="false"/>
            <xs:element name="PropertyC" type="xs:string" nillable="false"/>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="CustomType">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PropertyA" type="xs:dateTime"/>
                            <xs:element name="PropertyB" type="xs:double"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

With the XmlSerializer I am able to generate the code, but then I would have difficulties with the translation.

Could anyone tell me what I am doing wrong?
I would like to return an object with 3 properties and one Collection<CustomType>, but I am not sure how to handle my collection in the schema file.
I've been looking for an example but I can't find anything :-(

Thanks a lot
Marc

Developer
Jan 26, 2009 at 7:52 PM
Not sure if these links will be of some help but you can give them a shot:
http://kjellsj.blogspot.com/2008/03/wcf-datacontractserializer-schema-rules.html
http://blogs.thinktecture.com/cweyer/archive/2006/06/21/414623.aspx
http://msdn.microsoft.com/en-us/library/ms733112.aspx
Jan 27, 2009 at 8:09 AM
Hi,

http://msdn.microsoft.com/en-us/library/ms733112.aspx pointed me the right way.
Nested Sequences are not supported... as the errormessage already said :-)
So I had to change my schema to this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://someuri/commontypes" xmlns:mstns="http://someuri/commontypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://someuri/commontypes" elementFormDefault="qualified">
    <xs:element name="MytypeElement" type="Mytype"/>
    <xs:complexType name="Mytype">
        <xs:sequence>
            <xs:element name="PropertyA" type="xs:double" nillable="false"/>
            <xs:element name="PropertyB" type="xs:double" nillable="false"/>
            <xs:element name="PropertyC" type="xs:string" nillable="false"/>
            <xs:element name="people" type="PeopleElement"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="PeopleElement">
        <xs:sequence>
            <xs:element name="person" type="PersonElement" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="PersonElement">
        <xs:sequence>
             <xs:element name="PropertyA" type="xs:dateTime"/>
             <xs:element name="PropertyB" type="xs:double"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

I changed the names for a better understanding. "people" is my collection.
Now it is working!

Thanks
Marc
Jan 29, 2009 at 1:39 PM

How did you create the XSD?

Chad

Developer
Jan 29, 2009 at 3:14 PM
I guess that you can use svcutil (WCF) or xsd.exe (ASMX).
svcutil:
http://msdn.microsoft.com/en-us/library/aa347733.aspx
http://msdn.microsoft.com/en-us/library/aa702581.aspx
xsd:
http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.80).aspx

Jan 29, 2009 at 9:21 PM
I created the schema with Altova's XMLSpy.

I didn't know there is a tool for that...
Thanks :-)
Marc