Handling xs:choice

Apr 12, 2010 at 12:36 PM


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:sequence minOccurs="0" maxOccurs="unbounded">
				<xs:element name="Items" type="Shell"/>
	<xs:complexType name="Item">
			<xs:element name="Name"/>
	<xs:complexType name="SubItem">
			<xs:extension base="Item">
					<xs:element name="Colour"/>
	<xs:complexType name="Shell">
			<xs:element name="List" type="Item"/>
			<xs:element name="SubItem" type="SubItem"/>

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?
Apr 13, 2010 at 4: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.