共用方式為


Element Element Binding Support

The .NET Framework provides binding support for the <element> element.

Explanation

The XML Schema specification states that an element can be declared locally, within a complex type definition, or globally as a child of the root <schema> element. If the element is declared globally, it can be referenced from within one or more complex type definitions via the ref attribute.

The following code example shows an element declared locally:

<xsd:complexType name="UnitType">
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>

The following code example shows the same element declared globally and then referenced:

<xsd:element name="name" type="xsd:string"/>
<xsd:complexType name="UnitType">
  <xsd:sequence>
    <xsd:element ref="name"/>
  </xsd:sequence>
</xsd:complexType>

When generating a .NET Framework type from an XML Schema complex type, Xsd.exe does not differentiate between a locally declared element and a reference to a globally declared element, unless the global element is declared in a namespace other than the schema's target namespace.

Reference within the same namespace

Because Xsd.exe does not differentiate within the same namespace, a round-trip translation from XML schema to classes and back creates local elements that replace any references to global elements.

If the referenced global element's data type already has an automatic binding to a .NET Framework type, Xsd.exe does not generate a new type corresponding to the data type. As a result, not only is the global element declaration no longer referenced in the XML schema generated on the round trip, but it no longer appears there either. See Creation of global element declarations.

NoteNote

All built-in XML Schema data types have automatic bindings to .NET Framework types. In addition, schema-defined simple types, with one exception, bind to .NET Framework types rather than custom types. The single exception involves the enumeration of string-binding types; this exception is explained in more detail in the documentation for the <enumeration> element.

NoteNote

For more information about support for built-in types, see Data Type Support between XML Schema (XSD) Types and .NET Framework Types. For more information about support for schema-defined simple types, see the <restriction> element or any of the elements representing restriction facets.

Reference to another namespace

If a referenced global declaration belongs to a different namespace, then Xsd.exe specifies the namespace using the Namespace property of an XmlElementAttribute attribute that is applied to the generated field. For that particular element, the namespace specified via the Namespace property overrides the namespace specified at the class level using the XmlTypeAttribute attribute and, optionally, the XmlRootAttribute. An example follows:

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem")]
public string Text;

Additional namespaces are imported into an XML Schema definition using the <import> element.

Creation of global element declarations

When generating an XML Schema document from a set of classes in an assembly, Xsd.exe produces a global <element> declaration for every <complexType> or <simpleType> definition it produces from a type defined in the assembly.

Example

This first example shows how Xsd.exe processes a global element when that element is defined in the same target namespace that contains a reference to it.

Input XML Schema document:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="Text" type="xsd:normalizedString"/>
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

C# class generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" name="Text" type="xs:normalizedString" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

In the preceding generated XML schema, the Text element, originally declared globally, has been made a local element.

This second example shows how Xsd.exe processes a reference to a global element when that element is defined in a separate namespace. This example uses the <import> element to import a second namespace located in a separate XSD file. (The <import> element's schemaLocation attribute is not used to specify the location of the imported .xsd file. Instead, for Xsd.exe, the file is specified as an additional command-line argument.)

The top-level XML Schema document used as an input:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
      xmlns="http://example.org/" targetNamespace="http://example.org/" xmlns:elem="http://example.org/elem">
  <xsd:import  namespace="http://example.org/elem" />
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="elem:Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

The imported XML Schema document used as an input:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
      xmlns="http://example.org/elem" targetNamespace="http://example.org/elem">
  <xsd:element name="Text" type="xsd:normalizedString"/>
</xsd:schema>

C# class generated from the preceding two XML Schema documents:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem", DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

Top-level XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://example.org/elem" />
  <xs:element name="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" xmlns:q1="http://example.org/elem" ref="q1:Text" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

Imported XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/elem" elementFormDefault="qualified" targetNamespace="http://example.org/elem" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Text" type="xs:normalizedString" />
</xs:schema>

Abstract Attribute

A global element declaration can have an abstract attribute; a local element cannot.

Declare an element abstract (abstract="true") to force its substitution via a substitution group; if the element is not abstract, the substitution is optional.

The global element being substituted is called the head element. Wherever a local element declaration refers to the head element via the ref attribute, you can use any global substitute elements in its place. For more information, see the substitutionGroup attribute.

If the substitute elements belong to derived data types and if the head element itself is declared abstract (rather than the <complexType> definition for its type being declared abstract), you can use the base type elsewhere in an XML instance document, even though you cannot use it in the position where the substitution takes place.

When generating source code from an XML Schema document, Xsd.exe produces a type corresponding to the head element's data type. The type includes a field named Item. Xsd.exe applies to that field one XmlElementAttribute for each of the substitutes. Each attribute identifies the name and, if the substitute element has a derived data type, the type. If the substitute elements use the same data type as the head, an XmlChoiceIdentifierAttribute must also be applied. For more information, see the <choice> element.

The following code example shows a sample field, Item, generated from a reference to an abstract head element, :

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

If you are using substitute elements that belong to derived data types, XML serialization only works when an instance of one of the derived types has been assigned to the field. The XmlSerializer class fails if an instance of the base type corresponding to the head element has been assigned.

On a reverse translation, Xsd.exe produces a <choice> element containing an <element> element for each of the substitute elements, and no <element> representing the head element. The elements are independently defined within the complex type definition; no ref references are employed.

NoteNote

In a local <element> declaration containing a ref attribute specifying an abstract head element, Xsd.exe ignores the maxOccurs attribute.

NoteNote

Consider the following declaration to a head element of type MyBaseType:

NoteNote

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />

NoteNote

For the preceding declaration, Xsd.exe still produces the following field (attributes omitted):

NoteNote

public MyBaseType Item;

NoteNote

Xsd.exe fails to generate classes if all globally declared elements in the input XML schema are abstract.

Example: Abstract Attribute

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="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </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("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema> 

SubstitutionGroup Attribute

The Xsd.exe tool only recognizes substitution groups if the substituted element is declared abstract. (See the preceding section, Abstract Attribute.) Then, on a reverse translation, Xsd.exe produces a <choice> element containing an <element> element for each of the substitutes.

SubstitutionGroup Attribute: Background

The substitutionGroup attribute can appear with a globally declared element to allow it to take the place of another globally declared element in an XML instance document. The attribute value is the name of the element being substituted, called the head. The actual substitution can take place in an <element> element within a complex type definition, where the head element is referred to via the ref attribute. Any elements declared with the substitutionGroup attribute must belong to the same data type as the head element, or a derived type.

A substitution group is used to allow elements of different names, and optionally different types with the same base type, to appear in a specified position in an instance document.

Nested substitution groups are not supported. That is, if an element acts as the head element in one substitution group, it cannot be the substituted element in another substitution group. For example:

<xs:element name="Animal" abstract="true"/>
<xs:element name="Mammal" abstract="true" 
    substitutionGroup="tns:Animal"/>
<xs:element name="Dog" type="xs:string" 
    substitutionGroup="tns:Mammal"/>

This is not supported because Mammal acts as both the head (for Dog) and a substituted element (for Animal). If such a schema is encountered, Xsd.exe will not generate an XmlElementAttribute for Mammal, and Dog will not be where the Animal element is expected. However, you can manually add this to make this case work, as shown in the following example:

public class PetShop 
{
    private object Animal;
    
    // Apply the XmlElementAttribute to the generated property.
    [System.Xml.Serialization.XmlElementAttribute("Mammal"), 
     System.Xml.Serialization.XmlElementAttribute("Dog")]
    public object Animal {
        get {
            return this.Animal;
        }
        set {
            this.Animal = value;
        }
    }
}

SubstitutionGroup Attribute: Concrete head element

If the head element is not abstract, Xsd.exe takes each ref reference to the head and creates a field of the type corresponding to the head's XML data type (or an array of that type, depending on the value of the referring element's maxOccurs attribute). Any reference to the substitution group is lost.

While an object of a derived type could be assigned to that field and the XmlSerializer class would serialize that object, that behavior takes place outside of substitution with derived types. No substitute element name is used.

SubstitutionGroup Attribute: Abstract head element

If the head element is abstract, Xsd.exe produces a field of a type corresponding to the head's data type. The field receives the name Item. Xsd.exe applies to that field one XmlElementAttribute for each of the substitutes. Each attribute identifies the name and, if the substitute element has a derived data type, the type. If the substitute elements use the same data type as the head, an XmlChoiceIdentifierAttribute must also be applied. See the <choice> element.

Following is a sample field generated from a reference to an abstract head element:

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

If derived types are being used, XML serialization only works when an instance of one of the derived types has been assigned to the field. XmlSerializer fails if an instance of the base type corresponding to the head element has been assigned.

On a reverse translation, Xsd.exe produces from this construct a <choice> element containing an <element> element for each of the substitute elements, not for the head element. The elements are independently defined within the complex type definition; no ref references are employed.

maxOccurs attribute. For a local <element> declaration containing a ref attribute specifying an abstract head element, Xsd.exe ignores the maxOccurs attribute.

Consider the following declaration that refers to a head element of type MyBaseType:

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />

For the preceding declaration, Xsd.exe still produces the following field (attributes omitted):

public MyBaseType Item;

Example: SubstitutionGroup Attribute

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="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </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("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Possible Attributes Binding Support

abstract

The Xsd.exe utility binds substitution groups using abstract <element> declarations, but produces <choice> elements on a reverse translation.

See the preceding section, Abstract Attribute.

block

The block attribute can be used to prevent element declarations from being used as substitution group heads (the elements that get substituted by members of groups).

The Xsd.exe tool ignores the block attribute, as well as the blockDefault attribute of the Schema Element Binding Support element.

default

The default attribute provides a default value to be used if the element is empty in an instance document. If the element doesn't appear at all, it doesn't get filled in.

When generating source code from an XML Schema document, the Xsd.exe tool takes each field corresponding to an element with a default value and applies a System.ComponentModel.DefaultValueAttribute, passing the default value as an argument. In addition, Xsd.exe statically initializes the field to the default value, as in the following example:

[System.ComponentModel.DefaultValueAttribute(-1)]
public int age = -1;

When generating an <element> element from a class member that has a .NET Framework value type, Xsd.exe uses the DefaultValue attribute as an input in setting the minOccurs attribute. If Xsd.exe encounters a DefaultValue attribute applied to such a member, it produces a value of 0 for the element's minOccurs attribute. This indicates that the element does not need to appear in a valid XML instance document.

The default attribute is ignored on arrays (elements with maxOccurs greater than 1.)

See the Default Attribute Binding Support attribute.

final

The final attribute can be used to prevent element declarations from being used as substitution group heads (the elements that get substituted by members of groups).

Xsd.exe ignores the final attribute, as well as the finalDefault attribute of the <schema> element.

fixed

The fixed attribute provides a fixed value for the element in an instance document. When generating source code from an XML Schema document, Xsd.exe takes each field corresponding to an element with a fixed value and generates a static initializer for the field, as in the following example:

public int age = -1;

The fixed attribute is ignored on arrays (elements with maxOccurs greater than 1.)

form

The Xsd.exe tool equates the form XML attribute of the <element> element with the Form property of the XmlElementAttribute. The .NET Framework's XML serialization infrastructure recognizes a different default value, qualified.

If an <element> declaration in an XML schema specifies form="unqualified", Xsd.exe generates an XmlElement attribute for the corresponding field and passes the attribute a parameter, Form=XmlSchemaForm.Unqualified.

See the form attribute.

id

The Xsd.exe utility ignores the id attribute, which is intended to provide a unique identifier. Instead, Xsd.exe recognizes the name attribute.

maxOccurs

When generating classes from an XSD document, Xsd.exe interprets the maxOccurs attribute in the <element> element according to the following possible values:

  • 1: Xsd.exe produces a field of the type corresponding to the element's data type.

  • 0: Xsd.exe fails to process a value of 0, instead treating the value as the default 1.

  • unbounded: Xsd.exe produces a field that is an array of the type corresponding to the element's data type.

  • Any integer greater than 1: As with a value of unbounded, Xsd.exe produces a field that is an array of the type corresponding to the element's data type. You can enforce a value greater than 1 by validating an XML document with the XmlValidatingReader class against an XML Schema document represented by the SOM.

When generating an XML Schema document from a set of classes in an assembly, Xsd.exe reverses the preceding conversions, producing a maxOccurs value of 1 from a single instance and a maxOccurs value of unbounded from an array.

While Xsd.exe binds a maxOccurs value of unbounded to an array, it binds a maxOccurs value of 1 to the designated parent element, if any, of an array. See the MaxOccurs Attribute Binding Support attribute for more on the treatment of the maxOccurs element for array bindings, including the designation of an extra parent element.

minOccurs

For the <element> element, Xsd.exe checks the value of the minOccurs attribute only if the value of the MaxOccurs Attribute Binding Support attribute does not dictate an array field. Then, the value is interpreted (or produced) according to various factors, starting with whether the corresponding field has a reference or value type.

See the MinOccurs Attribute Binding Support attribute.

name

For generating source code from an XSD document, the value of the name attribute provides the name of the public class field that represents that element. If the <element> element contains an anonymous <complexType> definition, the name becomes the name of the class that corresponds to the complex type.

No attempt is made to change case to adhere to coding conventions. For example, if the name attribute of an <element> containing an anonymous complex type definition has the value testInfo, the ensuing class gets named testInfo, rather than the capitalized TestInfo. If a name conflicts with a reserved keyword, the resulting name is prepended with the symbol @.

When Xsd.exe generates an <element> declaration from a public class field, it uses the field name for the value of the name attribute. An alternate name—name attribute value—can be supplied via the following attribute properties:

See the Name Attribute Binding Support attribute.

nillable

The Xsd.exe tool equates the nillable attribute with the IsNullable property of certain XML serialization-related attributes applied to reference types. For value types, a nillable value of true will cause nullable types to be generated.

See the nillable attribute. The nillable attribute appears only in the <element> element.

ref

When generating a .NET Framework type from an XML Schema complex type, Xsd.exe does not differentiate between a locally declared element and a reference to a globally declared element, unless the global element is declared in a namespace other than the schema's target namespace.

See the sections Reference within the same namespace and Reference to another namespace.

substitutionGroup

The Xsd.exe tool only recognizes substitution groups if the substituted element is declared abstract. Then, on a reverse translation, Xsd.exe produces a <choice> element containing an <element> element for each of the substitutes.

See the preceding section, SubstitutionGroup Attribute.

type

The Xsd.exe tool associates the data types referenced with the type attribute of <element> and <attribute> declarations with .NET Framework types.

Xsd.exe does not generate a .NET Framework type for an XML schema data type unless the data type can be traced back to a global element declaration that refers to a data type (possibly another) via the type attribute.

Possible parent elements: <all>, <choice>, <schema>, <sequence>

Possible child elements: <annotation>, <complexType>, <key>, <keyref>, <simpleType>, <unique>

See Also

Reference

XmlSchemaElement