ComplexType Element Binding Support
The .NET Framework provides binding support for the <complexType> element.
The Xsd.exe tool equates XML Schema complex types with .NET Framework types whose public fields or properties represent the complex type's contents.
Explanation
The Xsd.exe tool equates XML Schema complex types with .NET Framework types whose public fields represent the complex type's contents.
Abstract Attribute
A complex type is declared abstract (abstract="true"
) to ensure that only instances of derived types, and not instances of the abstract base type, appear in a conformant XML document.
Xsd.exe equates an abstract complex type with an abstract class. The conversion works in both directions between code and XSD documents.
Aside from the use of the abstract keyword, the abstract class is treated like a non-abstract base class. Child classes extend the base class. Xsd.exe applies to the abstract class one System.Xml.Serialization.XmlIncludeAttribute for each descendant class. Each XmlInclude attribute specifies the type of the descendant class.
Example: Abstract Attribute
The following code example demonstrates using the abstract attribute with the <complexType> element.
Input XML Schema document:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
<xsd:element name="FamilyNode" type="FamilyNodeType" />
<xsd:complexType name="FamilyNodeType">
<xsd:sequence>
<xsd:element name="Code" type="xsd:string" />
<xsd:element name="Parent" type="Parent" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Daughter">
<xsd:complexContent>
<xsd:extension base="Parent">
<xsd:sequence>
<xsd:element name="Date" type="xsd:dateTime" minOccurs="0"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Son">
<xsd:complexContent>
<xsd:extension base="Parent">
<xsd:sequence>
<xsd:element name="Info" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Parent" abstract="true">
<xsd:sequence>
<xsd:element name="Text" type="xsd:normalizedString" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Grandchild">
<xsd:complexContent>
<xsd:extension base="Daughter">
<xsd:attribute name="Parent" type="xsd:normalizedString" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
C# classes generated from the preceding XML Schema document:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("FamilyNode", Namespace="http://example.org/", IsNullable=false)]
public class FamilyNodeType {
public string Code;
public Parent Parent;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Son))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Daughter))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public abstract class Parent {
[System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
public string Text;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Son : Parent {
public string Info;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public class Daughter : Parent {
public System.DateTime Date;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool DateSpecified;
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Grandchild : Daughter {
[System.Xml.Serialization.XmlAttributeAttribute("Parent", DataType="normalizedString")]
public string Parent1;
}
The XML schema generated from classes compiled from the preceding C# source code is effectively equivalent to the original XML schema.
Possible Attributes | Binding Support |
---|---|
abstract |
The Xsd.exe utility equates an abstract complex type, identified by See the preceding section, Abstract Attribute. |
block |
The block attribute can be applied to a data type to prevent derived types from taking the place of the original type where the original is specified. The Xsd.exe tool ignores the block attribute, as well as the blockDefault attribute of the Schema Element Binding Support element. |
final |
The final attribute can be applied to a data type to prevent it from being derived. Xsd.exe ignores the final attribute, as well as the finalDefault attribute of the <schema> element. |
id |
The Xsd.exe utility ignores the id attribute, which is intended to provide a unique identifier. Instead, Xsd.exe recognizes the name attribute. |
mixed |
See the mixed attribute. |
name |
The value of the name attribute becomes the name of the .NET Framework type that Xsd.exe generates from the complex type. No attempt is made to change case to adhere to coding conventions. For example, if the name attribute of a <complexType> element has the value When Xsd.exe generates a <complexType> definition from a class, it uses the class name for the value of the name attribute. An alternate name—name attribute value—can be supplied via the TypeName property. See the Name Attribute Binding Support attribute. |
Possible parent elements: <element>, <redefine>, <schema>
Possible child elements: <all>, <annotation>, <anyAttribute>, <attribute>, <attributeGroup>, <choice>, <complexContent>, <group>, <sequence>, <simpleContent>