Hello all,
I'm trying to design a schema from which I can generate a
typed dataset class. I'm having problems incorporating
choice and enumerations in the schema and getting the xml
results that I want. In the following example I want the
TopElement to contain either a Car element *or* a Truck
element, but not both. So I tried to use a Choice tag to
accomplish this. However, The Car element is of type
CarType which is a simpleType that contains an
enumeration. I used the enumeration so I could control the
valid values in the element.
When I generate a typed dataset using "xsd.exe /d", the
code creates a Car *and* a Truck element instead of a Car
*or* a Truck element.
So, the question is... Why is my schema not generating
the results that I want?
Here is a simplified example of what I want the resulting
xml to look like.
<?xml version="1.0" standalone="yes" ?>
- <SampleSchema
xmlns="http://tempuri.org/SampleSchema.xsd">
- <TopElement>
<Car>SportsCar</Car>
<Color>Blue</Color>
</TopElement>
</SampleSchema>
Here are the results that I'm getting
<?xml version="1.0" standalone="yes" ?>
- <SampleSchema
xmlns="http://tempuri.org/SampleSchema.xsd">
- <TopElement>
<Car>SportsCar</Car>
<Truck>RegularCab</Truck>
<Color>Blue</Color>
</TopElement>
</SampleSchema>
Here is the schema that I wrote:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="SampleSchema"
targetNamespace="http://tempuri.org/SampleSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/SampleSchema.xsd"
xmlns:mstns="http://tempuri.org/SampleSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TopElement"
type="TopElementType" />
<xs:complexType name="TopElementType">
<xs:sequence>
<xs:choice id="MyChoice" maxOccurs="1">
<xs:element name="Car" type="CarType"
maxOccurs="1" />
<xs:element name="Truck" type="TruckType"
maxOccurs="1" />
</xs:choice>
<xs:element name="Color" type="xs:string"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="CarType">
<xs:restriction base="xs:string">
<xs:enumeration
value="SportsCar" />
<xs:enumeration value="Sedan" />
<xs:enumeration value="Mini" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="TruckType">
<xs:restriction base="xs:string">
<xs:enumeration
value="RegularCab" />
<xs:enumeration value="SuperCab" />
<xs:enumeration value="LongBed" />
</xs:restriction>
</xs:simpleType>
</xs:schema>