public
is universally omitted.constructor
-- for clarity.static explicit operator
".explicit cast
.{ get; set; }
.class Foo : XTypedElement { constructor Foo(); explicit cast Foo(System.Xml.Linq.XElement xe); property string Bar { get; set; } }
Complex-type definitions are mapped to classes.
Each generated class comprises the following members:
XElement
to the defined class;Save
; see the LINQ to XSD manual;XTypedElement
.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A complex type for addresses --
>
<
xs:complexType name="Address">
<
!--
address details omitted --
>
<
/
xs:complexType>
<
/
xs:schema>
class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); }
We use the term "root" to refer to global element declarations in an XML schema.
We recall that a valid instance must be necessarily rooted in an element of that kind.
Such roots are essentially mapped in the same way as complex-type definitions.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A global element declaration for addresses --
>
<
xs:element name="Address">
<
xs:complexType>
<
!--
address details omitted --
>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); }
This pattern makes combined use of a complex-type definition and a global element declaration.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="BillTo" type="Address"/
>
<
xs:element name="ShipTo" type="Address"/
>
<
xs:complexType name="Address">
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
!--
... --
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
Content
member.class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); property string Name { get; set; } } class BillTo : XTypedElement { constructor BillTo(); constructor BillTo(Address content); explicit cast BillTo(System.Xml.Linq.XElement xe); property Address Content { get; } property string Name { get; set; } } class ShipTo : XTypedElement { constructor ShipTo(); constructor ShipTo(Address content); explicit cast ShipTo(System.Xml.Linq.XElement xe); property Address Content { get; } property string Name { get; set; } }
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:simpleType name="Month">
<
xs:restriction base="xs:token">
<
!--
restriction facets omitted --
>
<
/
xs:restriction>
<
/
xs:simpleType>
<
/
xs:schema>
Root element declarations (with anonymous types or not) are mapped to classes.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Month">
<
xs:simpleType>
<
xs:restriction base="xs:string">
<
!--
restriction facets omitted --
>
<
/
xs:restriction>
<
/
xs:simpleType>
<
/
xs:element>
<
/
xs:schema>
class Month : XTypedElement { constructor Month(); explicit cast Month(System.Xml.Linq.XElement xe); property string TypedValue { get; set; } }
TypedValue
property (akin to LINQ to XML's
untyped value property). The type of this property
is the built-in base type of the anonymous simple type. As usual,
the class also provides an explicit coercion (cast) from
XElement
to the class.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Month" type="MonthType"/
>
<
xs:simpleType name="MonthType">
<
xs:restriction base="xs:string">
<
!--
restriction facets omitted --
>
<
/
xs:restriction>
<
/
xs:simpleType>
<
/
xs:schema>
class Month : XTypedElement { constructor Month(); constructor Month(string content); explicit cast Month(System.Xml.Linq.XElement xe); property string TypedValue { get; set; } }
TypedValue
property (akin to LINQ to XML's untyped value property).
The type of this property is the OO counterpart for the built-in XSD base type of
the referenced simple type. As usual, the class also provides an explicit coercion
(cast) from XElement
to the class.
Characteristics of flat sequences:
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A very simple type of addresses --
>
<
xs:complexType name="Address">
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Zip" type="xs:int"/
>
<
xs:element name="Street" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); property string Name { get; set; } property int Zip { get; set; } property string Street { get; set; } }
Executive summary
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A host for the references to follow --
>
<
xs:complexType name="Customer">
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Age" type="AgeType"/
>
<
xs:element name="Address" type="AddressType"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
!--
A complex type to be referenced --
>
<
xs:complexType name="AddressType">
<
xs:sequence>
<
xs:element name="Zip" type="xs:int"/
>
<
xs:element name="Street" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
!--
A simple type to be referenced --
>
<
xs:simpleType name="AgeType">
<
xs:restriction base="xs:int">
<
xs:minInclusive value="0"/
>
<
xs:maxInclusive value="200"/
>
<
/
xs:restriction>
<
/
xs:simpleType>
<
/
xs:schema>
class Customer : XTypedElement { constructor Customer(); explicit cast Customer(System.Xml.Linq.XElement xe); property string Name { get; set; } property int Age { get; set; } property AddressType Address { get; set; } } class AddressType : XTypedElement { constructor AddressType(); explicit cast AddressType(System.Xml.Linq.XElement xe); property int Zip { get; set; } property string Street { get; set; } }
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A host for the references to follow --
>
<
xs:element name="Customer">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element ref="Age"/
>
<
xs:element ref="Address"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
!--
A referable element of a complex type --
>
<
xs:element name="Address">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="Zip" type="xs:int"/
>
<
xs:element name="Street" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
!--
A referable element of a simple type --
>
<
xs:element name="Age">
<
xs:simpleType>
<
xs:restriction base="xs:int">
<
xs:minInclusive value="0"/
>
<
xs:maxInclusive value="200"/
>
<
/
xs:restriction>
<
/
xs:simpleType>
<
/
xs:element>
<
/
xs:schema>
class Customer : XTypedElement { constructor Customer(); explicit cast Customer(System.Xml.Linq.XElement xe); property string Name { get; set; } property Age Age { get; set; } property Address Address { get; set; } } class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); property int Zip { get; set; } property string Street { get; set; } } class Age : XTypedElement { constructor Age(); explicit cast Age(System.Xml.Linq.XElement xe); property int TypedValue { get; set; } }
An element is optional, if the (local) element declaration is attributed as follows:
minOccurs="0"
.
maxOccurs
is 1 (which is the default).
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
Customer information with optional age and shipping-address information --
>
<
xs:complexType name="Customer">
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Age" type="xs:int" minOccurs="0"/
>
<
xs:element name="BillAddress" type="Address"/
>
<
xs:element name="ShipAddress" type="Address" minOccurs="0"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
!--
Part 1 of street address is required; part 2 is optional --
>
<
xs:complexType name="Address">
<
xs:sequence>
<
xs:element name="Zip" type="xs:int"/
>
<
xs:element name="Street1" type="xs:string"/
>
<
xs:element name="Street2" type="xs:string" minOccurs="0"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class Customer : XTypedElement { constructor Customer(); explicit cast Customer(System.Xml.Linq.XElement xe); property string Name { get; set; } property int? Age { get; set; } property Address BillAddress { get; set; } property Address ShipAddress { get; set; } } class Address : XTypedElement { constructor Address(); explicit cast Address(System.Xml.Linq.XElement xe); property int Zip { get; set; } property string Street1 { get; set; } property string Street2 { get; set; } }
An element is repeating, if the element declaration is attributed as follows:
maxOccurs
attribute is different from 0 and 1 (default).
minOccurs="0"
.
t
is result of mapping the element type of the particle.
IList
appled to t
.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A figure is a potentially empty list of lines. --
>
<
xs:element name="Figure">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="Line" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
!--
A line consists of 2 or more points. --
>
<
xs:element name="Line">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="Point" minOccurs="2" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Point">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="xCoord" type="xs:int"/
>
<
xs:element name="yCoord" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class Figure : XTypedElement { constructor Figure(); explicit cast Figure(System.Xml.Linq.XElement xe); property IList<Line> Line { get; set; } } class Line : XTypedElement { constructor Line(); explicit cast Line(System.Xml.Linq.XElement xe); property IList<Point> Point { get; set; } } class Point : XTypedElement { constructor Point(); explicit cast Point(System.Xml.Linq.XElement xe); property int xCoord { get; set; } property int yCoord { get; set; } }
Attribute declarations are mapped to properties that are added to the class that hosts the complex type of which the attributes are part of. (For simplicity, it is assumed that element and attribute names are distinct in a given scope. Otherwise, special rules of name mapping apply.)
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:complexType name="Product">
<
xs:sequence>
<
xs:element name="Number" type="xs:integer"/
>
<
xs:element name="Name" type="xs:string"/
>
<
/
xs:sequence>
<
xs:attribute name="EffDate" type="xs:date" use="optional"/
>
<
/
xs:complexType>
<
/
xs:schema>
class Product : XTypedElement { constructor Product(); explicit cast Product(System.Xml.Linq.XElement xe); property decimal Number { get; set; } property string Name { get; set; } property System.DateTime? EffDate { get; set; } }
class Product : XTypedElement { constructor Product(); explicit cast Product(System.Xml.Linq.XElement xe); property decimal Number { get; set; } property string Name { get; set; } property System.DateTime? EffDate { get; set; } }
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:complexType name="Product">
<
xs:sequence>
<
xs:element name="Number" type="xs:integer"/
>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="EffDate" type="xs:date" minOccurs="0"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
Characteristics of flat choices:
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="BillTo">
<
!--
Both street and pobox are of type xs:string. --
>
<
xs:complexType>
<
xs:choice>
<
xs:element name="Street" type="xs:string"/
>
<
xs:element name="Pobox" type="xs:string"/
>
<
/
xs:choice>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class BillTo : XTypedElement { constructor BillTo(); explicit cast BillTo(System.Xml.Linq.XElement xe); property string Street { get; set; } property string Pobox { get; set; } }
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
Arithmetic expression forms combined by choice --
>
<
xs:complexType name="Exp">
<
xs:choice>
<
xs:element name="Const" type="xs:int"/
>
<
xs:element name="Add" type="Add"/
>
<
/
xs:choice>
<
/
xs:complexType>
<
!--
Addition as an expression form --
>
<
xs:complexType name="Add">
<
xs:sequence>
<
xs:element name="Left" type="Exp"/
>
<
xs:element name="Right" type="Exp"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class Exp : XTypedElement { constructor Exp(); constructor Exp(int? Const); constructor Exp(Add Add); explicit cast Exp(System.Xml.Linq.XElement xe); property int? Const { get; set; } property Add Add { get; set; } } class Add : XTypedElement { constructor Add(); explicit cast Add(System.Xml.Linq.XElement xe); property Exp Left { get; set; } property Exp Right { get; set; } }
An element name is said to be recurrent in a given content model, if the content model comprises multiple element declarations of the given name. Recurrence is mapped such that all the relevant element declarations are mapped to a single property with a type as in the case of a repeating element particle. That is, there is no 1:1 correspondence of element particles and properties; instead there is 1:1 correspondence of element names and properties.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A figure as a list of lines --
>
<
xs:element name="Figure">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="Line" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
!--
A line that consist of two points. --
>
<
xs:element name="Line">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="Point"/
>
<
xs:element ref="Point"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Point">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="xCoord" type="xs:int"/
>
<
xs:element name="yCoord" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class Figure : XTypedElement { constructor Figure(); explicit cast Figure(System.Xml.Linq.XElement xe); property IList<Line> Line { get; set; } } class Line : XTypedElement { constructor Line(); explicit cast Line(System.Xml.Linq.XElement xe); property IList<Point> Point { get; set; } } class Point : XTypedElement { constructor Point(); explicit cast Point(System.Xml.Linq.XElement xe); property int xCoord { get; set; } property int yCoord { get; set; } }
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:complexType name="JobOffer">
<
xs:sequence>
<
xs:element name="JobId" type="xs:string"/
>
<
xs:sequence minOccurs="0" maxOccurs="unbounded">
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="SSN" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class JobOffer : XTypedElement { constructor JobOffer(); explicit cast JobOffer(System.Xml.Linq.XElement xe); property string JobId { get; set; } property IList<string> Name { get; set; } property IList<int> SSN { get; set; } }
The mapping for root elements with anonymous types was defined elsewhere. The mapping for local elements with anonymous simple types is trivial because the (derived) simple type or the type union or list type is reduced to the base built-in type as far as the API type is concerned. The case of local elements with anonymous complex types remains.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Bib">
<
xs:complexType>
<
xs:sequence>
<
xs:element maxOccurs="unbounded" name="Book">
<
xs:complexType>
<
xs:sequence>
<
xs:choice maxOccurs="unbounded">
<
xs:element name="Title" type="xs:string"/
>
<
xs:element maxOccurs="unbounded" name="Author">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="Last" type="xs:string"/
>
<
xs:element name="First" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Editor">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="Last" type="xs:string"/
>
<
xs:element name="First" type="xs:string"/
>
<
xs:element name="Affiliation">
<
xs:complexType>
<
xs:simpleContent>
<
xs:extension base="xs:string">
<
xs:attribute name="Type" type="xs:string" use="required"/
>
<
/
xs:extension>
<
/
xs:simpleContent>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Publisher" type="xs:string"/
>
<
xs:element name="Price" type="xs:decimal"/
>
<
/
xs:choice>
<
/
xs:sequence>
<
xs:attribute name="Year" type="xs:unsignedShort" use="required"/
>
<
xs:attribute name="Anson" type="xs:boolean" use="optional"/
>
<
xs:attribute name="Joe" type="xs:string" use="optional"/
>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class Bib : XTypedElement { constructor Bib(); explicit cast Bib(System.Xml.Linq.XElement xe); class BookLocalType : XTypedElement { constructor BookLocalType(); explicit cast BookLocalType(System.Xml.Linq.XElement xe); class AuthorLocalType : XTypedElement { constructor AuthorLocalType(); explicit cast AuthorLocalType(System.Xml.Linq.XElement xe); property string Last { get; set; } property string First { get; set; } } class EditorLocalType : XTypedElement { constructor EditorLocalType(); explicit cast EditorLocalType(System.Xml.Linq.XElement xe); class AffiliationLocalType : XTypedElement { constructor AffiliationLocalType(); explicit cast AffiliationLocalType(System.Xml.Linq.XElement xe); property string TypedValue { get; set; } property string Type { get; set; } } property string Last { get; set; } property string First { get; set; } property AffiliationLocalType Affiliation { get; set; } } property IList<string> Title { get; set; } property IList<AuthorLocalType> Author { get; set; } property IList<EditorLocalType> Editor { get; set; } property IList<string> Publisher { get; set; } property IList<decimal> Price { get; set; } property ushort Year { get; set; } property bool? Anson { get; set; } property string Joe { get; set; } } property IList<BookLocalType> Book { get; set; } }
class Bib : XTypedElement { constructor Bib(); explicit cast Bib(System.Xml.Linq.XElement xe); property IList<Book> Book { get; set; } } class Book : XTypedElement { constructor Book(); explicit cast Book(System.Xml.Linq.XElement xe); property IList<string> Title { get; set; } property IList<author> author { get; set; } property IList<Editor> Editor { get; set; } property IList<string> Publisher { get; set; } property IList<decimal> Price { get; set; } property ushort Year { get; set; } property bool? Anson { get; set; } property string Joe { get; set; } } class author : XTypedElement { constructor author(); explicit cast author(System.Xml.Linq.XElement xe); property string Last { get; set; } property string First { get; set; } } class Editor : XTypedElement { constructor Editor(); explicit cast Editor(System.Xml.Linq.XElement xe); property string Last { get; set; } property string First { get; set; } property Affiliation Affiliation { get; set; } } class Affiliation : XTypedElement { constructor Affiliation(); explicit cast Affiliation(System.Xml.Linq.XElement xe); property string TypedValue { get; set; } property string Type { get; set; } }
Note: The current release of LINQ to XSD provides incomplete/disabled support for fixed and default values. At the time of writing, default attributes were still to be expected for the release, but all other forms of fixed and default values were not expected for the release.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:complexType name="ProductType">
<
xs:sequence>
<
xs:element name="Number" type="xs:string"/
>
<
xs:element name="Name" type="xs:string"/
>
<
xs:any minOccurs="0" maxOccurs="unbounded" namespace="##other" processContents="lax"/
>
<
/
xs:sequence>
<
xs:anyAttribute namespace="##other" processContents="skip"/
>
<
/
xs:complexType>
<
/
xs:schema>
class ProductType : XTypedElement { constructor ProductType(); explicit cast ProductType(System.Xml.Linq.XElement xe); property string Number { get; set; } property string Name { get; set; } property IEnumerable<System.Xml.Linq.XElement> Any { get; } }
Any
property is always of type XElement
or
a list type thereof. Occurrence constraints and recurrence of
Any
are subject to the same rules as for normal element
particles. That is, if the Any
is neither repeating nor
recurring, then the generated property is of type XElement
.
Otherwise, the property is of the list type.
For simplicity, the following assumptions are made:
block
and final
are not used.<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A base type of products --
>
<
xs:complexType name="Product">
<
xs:sequence>
<
xs:element name="Number" type="xs:integer"/
>
<
xs:element name="Name" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
!--
shirt extends product --
>
<
xs:complexType name="Shirt">
<
xs:complexContent>
<
xs:extension base="Product">
<
xs:sequence>
<
xs:element name="Size" type="xs:string"/
>
<
xs:element name="Color" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
!--
hat extends product --
>
<
xs:complexType name="Hat">
<
xs:complexContent>
<
xs:extension base="Product">
<
xs:sequence>
<
xs:element name="Size" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
/
xs:schema>
class Product : XTypedElement { constructor Product(); explicit cast Product(System.Xml.Linq.XElement xe); property decimal Number { get; set; } property string Name { get; set; } } class Shirt : Product { constructor Shirt(); explicit cast Shirt(System.Xml.Linq.XElement xe); property string Size { get; set; } property string Color { get; set; } } class Hat : Product { constructor Hat(); explicit cast Hat(System.Xml.Linq.XElement xe); property int Size { get; set; } }
Note: This topic is not documented in the current release of LINQ to XSD.
Note: This topic is not documented in the current release of LINQ to XSD.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
A base type of products --
>
<
xs:complexType name="Product">
<
xs:sequence>
<
xs:element name="Number" type="xs:integer"/
>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Size" type="xs:int" minOccurs="0"/
>
<
xs:element name="Color" type="xs:string" minOccurs="0"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
!--
A restriction of product --
>
<
xs:complexType name="RigidProduct">
<
xs:complexContent>
<
xs:restriction base="Product">
<
xs:sequence>
<
xs:element name="Number" type="xs:integer"/
>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Size" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:restriction>
<
/
xs:complexContent>
<
/
xs:complexType>
<
/
xs:schema>
class Product : XTypedElement { constructor Product(); explicit cast Product(System.Xml.Linq.XElement xe); property decimal Number { get; set; } property string Name { get; set; } property int? Size { get; set; } property string Color { get; set; } } class RigidProduct : Product { constructor RigidProduct(); explicit cast RigidProduct(System.Xml.Linq.XElement xe); }
Here is an example for using substitution groups.
(We continue the example on type derivation by extension for complex types.)
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Product" type="ProductType"/
>
<
xs:element name="Shirt" type="ShirtType"/
>
<
xs:element name="Hat" type="HatType"/
>
<
xs:complexType name="ProductType">
<
xs:sequence>
<
!--
product details omitted --
>
<
/
xs:sequence>
<
/
xs:complexType>
<
xs:complexType name="ShirtType">
<
xs:complexContent>
<
xs:extension base="ProductType">
<
xs:sequence>
<
!--
shirt details omitted --
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="HatType">
<
xs:complexContent>
<
xs:extension base="ProductType">
<
xs:sequence>
<
!--
hat details omitted --
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
/
xs:schema>
class ProductType : XTypedElement { constructor ProductType(); explicit cast ProductType(System.Xml.Linq.XElement xe); } class ShirtType : ProductType { constructor ShirtType(); explicit cast ShirtType(System.Xml.Linq.XElement xe); } class HatType : ProductType { constructor HatType(); explicit cast HatType(System.Xml.Linq.XElement xe); } class Product : XTypedElement { constructor Product(); constructor Product(ProductType content); explicit cast Product(System.Xml.Linq.XElement xe); property ProductType Content { get; } } class Shirt : XTypedElement { constructor Shirt(); constructor Shirt(ShirtType content); explicit cast Shirt(System.Xml.Linq.XElement xe); property ShirtType Content { get; } } class Hat : XTypedElement { constructor Hat(); constructor Hat(HatType content); explicit cast Hat(System.Xml.Linq.XElement xe); property HatType Content { get; } }
Note: This topic is not documented in the current release of LINQ to XSD.
These definitions are not mapped. References to these definitions are mapped by inlining them in the place of the reference. Note: This approach may be refined in a future version of LINQ to XSD so that groups are mapped for better code factorization.
These declarations are not mapped.
References to these declarations are mapped by inlining them in the place of the reference.
These definitions are not mapped.
References to these definitions are mapped by inlining them in the place of the reference.
The System.Xml.Schema semantics of redefinitions applies.
Essentially, this implies that redefinitions override the original definitions.
Identity constraints are not taken into account, in any way.
Normal validation is supposed to check these constraints.
There are no designated API members for mixed content.
Interim text is discoverable and definable through the XElement interface.
Tool tips may document the mixed-content status of a class.
By default, each and every XSD built-in simple type is mapped to a CLR type, mostly to a CLR value type or to string. This default mapping is exercised below by means of a content model with an element particle for each and every built-in type.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
!--
An illustrative type that exercises all built-in simple types. --
>
<
!--
Indeed, all these types are listed in alphabetic order. --
>
<
xs:complexType name="simpleType">
<
xs:sequence>
<
xs:element name="xsAnySimpleType" type="xs:anySimpleType"/
>
<
xs:element name="xsAnyURI" type="xs:anyURI"/
>
<
xs:element name="xsBase64Binary" type="xs:base64Binary"/
>
<
xs:element name="xsBoolean" type="xs:boolean"/
>
<
xs:element name="xsByte" type="xs:byte"/
>
<
xs:element name="xsDate" type="xs:date"/
>
<
xs:element name="xsDateTime" type="xs:dateTime"/
>
<
xs:element name="xsENTITIES" type="xs:ENTITIES"/
>
<
xs:element name="xsENTITY" type="xs:ENTITY"/
>
<
xs:element name="xsFloat" type="xs:float"/
>
<
xs:element name="xsDecimal" type="xs:decimal"/
>
<
xs:element name="xsDouble" type="xs:double"/
>
<
xs:element name="xsDuration" type="xs:duration"/
>
<
xs:element name="xsGDay" type="xs:gDay"/
>
<
xs:element name="xsGMonth" type="xs:gMonth"/
>
<
xs:element name="xsGMonthDay" type="xs:gMonthDay"/
>
<
xs:element name="xsGYear" type="xs:gYear"/
>
<
xs:element name="xsGYearMonth" type="xs:gYearMonth"/
>
<
xs:element name="xsHexBinary" type="xs:hexBinary"/
>
<
xs:element name="xsID" type="xs:ID"/
>
<
xs:element name="xsIDREF" type="xs:IDREF"/
>
<
xs:element name="xsIDREFS" type="xs:IDREFS"/
>
<
xs:element name="xsInt" type="xs:int"/
>
<
xs:element name="xsInteger" type="xs:integer"/
>
<
xs:element name="xsLanguage" type="xs:language"/
>
<
xs:element name="xsLong" type="xs:long"/
>
<
xs:element name="xsName" type="xs:Name"/
>
<
xs:element name="xsNCName" type="xs:NCName"/
>
<
xs:element name="xsNegativeInteger" type="xs:negativeInteger"/
>
<
xs:element name="xsNMTOKEN" type="xs:NMTOKEN"/
>
<
xs:element name="xsNMTOKENS" type="xs:NMTOKENS"/
>
<
xs:element name="xsNonNegativeInteger" type="xs:nonNegativeInteger"/
>
<
xs:element name="xsNonPositiveInteger" type="xs:nonPositiveInteger"/
>
<
xs:element name="xsNormalizedString" type="xs:normalizedString"/
>
<
!--
xs:NOTATION was omitted since it is only used in derivations. --
>
<
xs:element name="xsPositiveInteger" type="xs:positiveInteger"/
>
<
xs:element name="xsQName" type="xs:QName"/
>
<
xs:element name="xsUnsignedByte" type="xs:unsignedByte"/
>
<
xs:element name="xsUnsignedInt" type="xs:unsignedInt"/
>
<
xs:element name="xsUnsignedLong" type="xs:unsignedLong"/
>
<
xs:element name="xsUnsignedShort" type="xs:unsignedShort"/
>
<
xs:element name="xsShort" type="xs:short"/
>
<
xs:element name="xsString" type="xs:string"/
>
<
xs:element name="xsTime" type="xs:time"/
>
<
xs:element name="xsToken" type="xs:token"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class simpleType : XTypedElement { constructor simpleType(); explicit cast simpleType(System.Xml.Linq.XElement xe); property string xsAnySimpleType { get; set; } property System.Uri xsAnyURI { get; set; } property System.Byte[] xsBase64Binary { get; set; } property bool xsBoolean { get; set; } property sbyte xsByte { get; set; } property System.DateTime xsDate { get; set; } property System.DateTime xsDateTime { get; set; } property IList<string> xsENTITIES { get; set; } property string xsENTITY { get; set; } property float xsFloat { get; set; } property decimal xsDecimal { get; set; } property double xsDouble { get; set; } property System.TimeSpan xsDuration { get; set; } property System.DateTime xsGDay { get; set; } property System.DateTime xsGMonth { get; set; } property System.DateTime xsGMonthDay { get; set; } property System.DateTime xsGYear { get; set; } property System.DateTime xsGYearMonth { get; set; } property System.Byte[] xsHexBinary { get; set; } property string xsID { get; set; } property string xsIDREF { get; set; } property IList<string> xsIDREFS { get; set; } property int xsInt { get; set; } property decimal xsInteger { get; set; } property string xsLanguage { get; set; } property long xsLong { get; set; } property string xsName { get; set; } property string xsNCName { get; set; } property decimal xsNegativeInteger { get; set; } property string xsNMTOKEN { get; set; } property IList<string> xsNMTOKENS { get; set; } property decimal xsNonNegativeInteger { get; set; } property decimal xsNonPositiveInteger { get; set; } property string xsNormalizedString { get; set; } property decimal xsPositiveInteger { get; set; } property System.Xml.XmlQualifiedName xsQName { get; set; } property byte xsUnsignedByte { get; set; } property uint xsUnsignedInt { get; set; } property ulong xsUnsignedLong { get; set; } property ushort xsUnsignedShort { get; set; } property short xsShort { get; set; } property string xsString { get; set; } property System.DateTime xsTime { get; set; } property string xsToken { get; set; } }
Note: This topic is not documented in the current release of LINQ to XSD.
Note: This topic is not documented in the current release of LINQ to XSD.
The following schema is used in the LINQ to XSD overview.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Batch">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="PurchaseOrder" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="PurchaseOrder">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="CustId" type="xs:string"/
>
<
xs:element ref="Item" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Item">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="ProdId" type="xs:string"/
>
<
xs:element name="Price" type="xs:double"/
>
<
xs:element name="Quantity" type="xs:int"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
/
xs:schema>
class Batch : XTypedElement { constructor Batch(); explicit cast Batch(System.Xml.Linq.XElement xe); property IList<PurchaseOrder> PurchaseOrder { get; set; } } class PurchaseOrder : XTypedElement { constructor PurchaseOrder(); explicit cast PurchaseOrder(System.Xml.Linq.XElement xe); property string CustId { get; set; } property IList<Item> Item { get; set; } } class Item : XTypedElement { constructor Item(); explicit cast Item(System.Xml.Linq.XElement xe); property string ProdId { get; set; } property double Price { get; set; } property int Quantity { get; set; } }
The following schema is used in the XML 2006 paper.
<
:schema xmlns:xmlns="http://www.w3.org/2001/XMLSchema">
<
:element name="Invoice">
<
:complexType>
<
:sequence>
<
:element name="Name" type="string"/
>
<
:element name="Street" type="string"/
>
<
:element name="City" type="string"/
>
<
:element name="Zip" type="string"/
>
<
:element name="State" type="string"/
>
<
:element name="Position" maxOccurs="unbounded">
<
:complexType>
<
:sequence>
<
:element name="ProdId" type="string"/
>
<
:element name="Price" type="double"/
>
<
:element name="Quantity" type="int"/
>
<
/
:sequence>
<
/
:complexType>
<
/
:element>
<
:element name="Total" type="double"/
>
<
/
:sequence>
<
/
:complexType>
<
/
:element>
<
/
:schema>
class Invoice : XTypedElement { constructor Invoice(); explicit cast Invoice(System.Xml.Linq.XElement xe); property string Name { get; set; } property string Street { get; set; } property string City { get; set; } property string Zip { get; set; } property string State { get; set; } property IList<Position> Position { get; set; } property double Total { get; set; } } class Position : XTypedElement { constructor Position(); explicit cast Position(System.Xml.Linq.XElement xe); property string ProdId { get; set; } property double Price { get; set; } property int Quantity { get; set; } }
The following schema is used in the LINQ to XSD demo.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Company">
<
xs:complexType>
<
xs:sequence>
<
xs:element ref="Department" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Department">
<
xs:complexType>
<
xs:sequence>
<
xs:element name="Label" type="xs:string"/
>
<
xs:element name="Manager" type="EmployeeType" minOccurs="0"/
>
<
xs:element ref="Member" minOccurs="0" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
xs:attribute name="Deptno" type="xs:int"/
>
<
/
xs:complexType>
<
/
xs:element>
<
xs:element name="Member">
<
xs:complexType>
<
xs:choice>
<
xs:element name="Employee" type="EmployeeType"/
>
<
xs:element ref="Department"/
>
<
/
xs:choice>
<
/
xs:complexType>
<
/
xs:element>
<
xs:complexType name="EmployeeType">
<
xs:sequence>
<
xs:element name="Name" type="xs:string"/
>
<
xs:element name="Salary" type="xs:double"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
/
xs:schema>
class Company : XTypedElement { constructor Company(); explicit cast Company(System.Xml.Linq.XElement xe); property IList<Department> Department { get; set; } } class Department : XTypedElement { constructor Department(); explicit cast Department(System.Xml.Linq.XElement xe); property string Label { get; set; } property EmployeeType Manager { get; set; } property IList<Member> Member { get; set; } property int? Deptno { get; set; } } class Member : XTypedElement { constructor Member(); constructor Member(EmployeeType Employee); constructor Member(Department Department); explicit cast Member(System.Xml.Linq.XElement xe); property EmployeeType Employee { get; set; } property Department Department { get; set; } } class EmployeeType : XTypedElement { constructor EmployeeType(); explicit cast EmployeeType(System.Xml.Linq.XElement xe); property string Name { get; set; } property double Salary { get; set; } }
The following schema is used in the LINQ to XSD demo.
<
xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<
xs:element name="Program" type="Block"/
>
<
xs:complexType name="Expr" abstract="true"/
>
<
xs:complexType name="Constant">
<
xs:complexContent>
<
xs:extension base="Expr">
<
xs:choice>
<
xs:element name="Integer" type="xs:int"/
>
<
xs:element name="Boolean" type="xs:boolean"/
>
<
/
xs:choice>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="Variable">
<
xs:complexContent>
<
xs:extension base="Expr">
<
xs:sequence>
<
xs:element name="Id" type="xs:string"/
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="Block">
<
xs:complexContent>
<
xs:extension base="Expr">
<
xs:sequence>
<
xs:element name="Decl" type="Decl" minOccurs="0" maxOccurs="unbounded"/
>
<
xs:element name="Expr" type="Expr" maxOccurs="unbounded"/
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="Decl">
<
xs:sequence>
<
xs:element name="Id" type="xs:string"/
>
<
xs:element name="Type" type="Type"/
>
<
/
xs:sequence>
<
/
xs:complexType>
<
xs:complexType name="Type" abstract="true"/
>
<
xs:complexType name="Integer">
<
xs:complexContent>
<
xs:extension base="Type"/
>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="Boolean">
<
xs:complexContent>
<
xs:extension base="Type"/
>
<
/
xs:complexContent>
<
/
xs:complexType>
<
xs:complexType name="Assign">
<
xs:complexContent>
<
xs:extension base="Expr">
<
xs:sequence>
<
xs:element name="Id" type="xs:string"/
>
<
xs:element name="Rhs" type="Expr"/
>
<
/
xs:sequence>
<
/
xs:extension>
<
/
xs:complexContent>
<
/
xs:complexType>
<
/
xs:schema>
class Expr : XTypedElement { constructor Expr(); explicit cast Expr(System.Xml.Linq.XElement xe); } class Constant : Expr { constructor Constant(); constructor Constant(int? Integer); constructor Constant(bool? Boolean); explicit cast Constant(System.Xml.Linq.XElement xe); property int? Integer { get; set; } property bool? Boolean { get; set; } } class Variable : Expr { constructor Variable(); explicit cast Variable(System.Xml.Linq.XElement xe); property string Id { get; set; } } class Block : Expr { constructor Block(); explicit cast Block(System.Xml.Linq.XElement xe); property IList<Decl> Decl { get; set; } property IList<Expr> Expr { get; set; } } class Decl : XTypedElement { constructor Decl(); explicit cast Decl(System.Xml.Linq.XElement xe); property string Id { get; set; } property Type Type { get; set; } } class Type : XTypedElement { constructor Type(); explicit cast Type(System.Xml.Linq.XElement xe); } class Integer : Type { constructor Integer(); explicit cast Integer(System.Xml.Linq.XElement xe); } class Boolean : Type { constructor Boolean(); explicit cast Boolean(System.Xml.Linq.XElement xe); } class Assign : Expr { constructor Assign(); explicit cast Assign(System.Xml.Linq.XElement xe); property string Id { get; set; } property Expr Rhs { get; set; } } class Program : XTypedElement { constructor Program(); constructor Program(Block content); explicit cast Program(System.Xml.Linq.XElement xe); property Block Content { get; } property IList<Decl> Decl { get; set; } property IList<Expr> Expr { get; set; } }