Handling xs:choice

Apr 12, 2010 at 11:36 AM

Hi,

We're using LinqToXSD in our project and we're trying to figure out the "right way" to use LinqToXSD's implementation of the xs:choice element.

The following schema tried to implement a list of types that all inherit from a base type:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="List">
		<xs:complexType>
			<xs:sequence minOccurs="0" maxOccurs="unbounded">
				<xs:element name="Items" type="Shell"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:complexType name="Item">
		<xs:sequence>
			<xs:element name="Name"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="SubItem">
		<xs:complexContent>
			<xs:extension base="Item">
				<xs:sequence>
					<xs:element name="Colour"/>
				</xs:sequence>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>
	<xs:complexType name="Shell">
		<xs:choice>
			<xs:element name="List" type="Item"/>
			<xs:element name="SubItem" type="SubItem"/>
		</xs:choice>
	</xs:complexType>
</xs:schema>

But the LinqToXSD implementation of an Shell is bizarre. It contains two properties, one for Item and one for SubItem.
It's really us trying to get around the limitation of Schema not allowing us to create sequences of the base type Item, while also allowing the extended type SubItem in the list. It just doesn't allow polymorphism.
The closes I've ever found is this example: http://www.datypic.com/books/defxmlschema/chapter14.html
Which I used to create the above schema.
In the above schema, in order to preserve the ordering of the elements I wrap the choice in a Shell type.
In the linked document, they have an unbounded choice element.
This unbounded choice creates a class with one IList<T> of each of the allowed choices.
How are you supposed to extract the sequencing information from this? How to I tell which order the elements are added to the document?
Thanks,
James.
Developer
Apr 13, 2010 at 3:18 PM

Hi James,

You may use the Untyped property. For example, use shell.Untyped.Nodes instead of shell.List, shell.SubItem.

Best regards,

Sergey Shandar.