Freigeben über


Schema Element Binding Support 

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

The <schema> element is the root element of an XML Schema document.

Explanation

The <schema> element is the root element of an XML Schema document.

Of the <schema> element's attributes, the blockDefault, finalDefault, id and version attributes do not have bindings to .NET Framework code entities. The targetNamespace attribute is recognized; the attributeFormDefault and elementFormDefault attributes are partially recognized. See the following table. For details, refer to the individual attribute topics.

TargetNamespace Attribute

The targetNamespace attribute of the root <schema> element specifies the namespace of the schema as a Uniform Resource Identifier (URI). The Xsd.exe tool relates the targetNamespace attribute of the <schema> element to Namespace properties of various XML-related attributes.

When generating source code from an XML Schema document, Xsd.exe uses the targetNamespace value as follows:

  • For every type generated that corresponds to a data type in the schema, Xsd.exe sets the XmlTypeAttribute.Namespace property to the targetNamespace value.

  • For every type generated that corresponds to a data type that might appear as the root of a schema-conformant XML document, Xsd.exe sets the XmlRootAttribute.Namespace property to the targetNamespace value. A data type can appear as a document root if it is used by an <element> element that is declared globally (as a child of the root <schema> element).

When generating an XML Schema document from a set of classes in an assembly, Xsd.exe creates an XML schema document with a unique targetNamespace attribute for each unique value it finds in the Namespace property of certain XML-related attribute classes. The property appears as follows:

AttributeFormDefault Attribute

The Xsd.exe utility generates XML schemas that leave the global attributeFormDefault attribute unspecified. When it encounters the attribute in an existing schema, it sets the Form property of each non-overridden field's XmlAttributeAttribute accordingly.

Explanation: AttributeFormDefault Attribute

The XML Schema requires that all elements and attributes that are globally declared (as children of the <schema> element) appear namespace-qualified in an instance document. For elements and attributes declared locally (within a <complexType> definition), namespace qualification is optional.

To control namespace qualification for all local elements and attributes declared in an XML Schema document, use the elementFormDefault and attributeFormDefault attributes, respectively, of the root <schema> element. For both, the possible values are qualified and unqualified, with the default being unqualified.

In addition, the form attribute can be used on a local <element> or <attribute> declaration to override the schema default for that particular element or attribute.

The following table describes how Xsd.exe processes the three possible choices for attributeFormDefault: qualified, unqualified, not specified. (Remember that Xsd.exe, when generating source code from an XML Schema document, creates a public field for each XML attribute and applies to that field a .NET Framework attribute, XmlAttributeAttribute.)

Possible attributeFormDefault values

Source generated by Xsd.exe from XSD

XSD generated by Xsd.exe from assembly compiled from same source

attributeFormDefault="unqualified"

Classes and the fields corresponding to attributes appear without form specified.

Not specified, reverts to the default attributeFormDefault="unqualified".

Unspecified

Classes and the fields corresponding to attributes appear without form specified.

Not specified, reverts to the default attributeFormDefault="unqualified".

attributeFormDefault="qualified"

Fields corresponding to both locally declared attributes and referenced globally declared attributes appear with an XmlAttribute attribute that is passed Form=XmlSchemaForm.Qualified.

Not specified, reverts to the default attributeFormDefault="unqualified".

Additionally, each attribute declaration specifies form="qualified". This includes referenced globally declared attributes.

The parameter passed to the XmlAttribute declaration sets the Form property, the possible values for which come from the XmlSchemaForm enumeration:

  • XmlSchemaForm.Qualified

  • XmlSchemaForm.Unqualified

  • XmlSchemaForm.None: the default

One outcome of using an XmlAttribute parameter is that what had been a global setting in the original XSD document gets specified on a per-XML-attribute basis in the source code and the generated XSD. The global attributeFormDefault attribute is always assumed to be unspecified, reverting to the default value of unqualified.

Example: AttributeFormDefault Attribute

Input XML Schema document where attributeFormDefault="qualified":

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified"
            xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
  <xsd:attribute name="version" type="xsd:string"/>
  <xsd:complexType name="keyInfo">
    <xsd:attribute ref="version" />
    <xsd:attribute name="public" type="xsd:boolean" use="required"/>
  </xsd:complexType>
  <xsd:element name="key" type="keyInfo"/>
</xsd:schema>

C# class generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("key", Namespace="http://example.org/", IsNullable=false)]
public class keyInfo {
    [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
    public string version;
        
    System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
    public bool @public;
}

XML Schema root <schema> element generated from an assembly compiled from the preceding C# source (attribute declarations also appear without form attribute):

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   ...
</xs:schema>

ElementFormDefault Attribute

The Xsd.exe utility generates XML schemas that leave the global elementFormDefault attribute set to "qualified". When it encounters the attribute in an existing schema, it sets the Form property of each non-overridden field's XmlElementAttribute accordingly.

Explanation: ElementFormDefault Attribute

The XML Schema requires that all elements and attributes that are globally declared (as children of the <schema> element) appear namespace-qualified in an instance document. For elements and attributes declared locally (within a <complexType> definition), namespace qualification is optional.

To control namespace qualification for all local elements and attributes declared in an XML Schema document, use the elementFormDefault and attributeFormDefault attributes, respectively, of the root <schema> element. For both, the possible values are qualified and unqualified, with the default being unqualified.

In addition, the form attribute can be used on a local <element> or <attribute> declaration to override the schema default for that particular element or attribute.

While the default value of elementFormDefault is unqualified, the .NET Framework effectively treats the default as qualified, while still honoring the use of unqualified. The following table describes how Xsd.exe processes the three possible choices for elementFormDefault: qualified, unqualified, not specified. (Remember that Xsd.exe, when generating source code from an XML Schema document, creates a public field for each XML element.)

Possible elementFormDefault values

Source generated by Xsd.exe from XSD

XSD generated by Xsd.exe from assembly compiled from same source

elementFormDefault="qualified"

Classes and the fields corresponding to elements appear without form specified.

elementFormDefault="qualified"

Unspecified

Classes and the fields corresponding to elements appear without form specified.

elementFormDefault="qualified"

elementFormDefault="unqualified"

Fields corresponding to locally declared elements appear with an XmlElement attribute that is passed Form=XmlSchemaForm.Unqualified. Fields corresponding to referenced globally declared elements appear without form specified.

elementFormDefault="qualified"

Additionally, each element that originally had been locally declared uses form="unqualified". Elements that originally had been globally declared appear locally without a form attribute.

The parameter passed to the XmlElement declaration sets the Form property, the possible values for which come from the XmlSchemaForm enumeration:

  • XmlSchemaForm.Qualified

  • XmlSchemaForm.Unqualified

  • XmlSchemaForm.None: the default

One outcome of using an XmlElement parameter is that what had been a global setting in the original XSD document gets specified on a per-XML-element basis in the source code and the generated XSD. The global elementFormDefault attribute is always assumed to have the value qualified.

Example: ElementFormDefault Attribute

Input XML Schema document where elementFormDefault="unqualified":

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="unqualified">
  <xsd:element name="version" type="xsd:string"/>
  <xsd:complexType name="keyInfo">
      <xsd:sequence>
      <xsd:element ref="version" />
      <xsd:element name="public" type="xsd:boolean" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="key" type="keyInfo"/>
</xsd:schema>

C# class generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("key", Namespace="http://example.org/", IsNullable=false)]
 public class keyInfo {
        
     public string version;
        
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public bool @public;
}

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="key" type="tns:keyInfo" />
  <xs:complexType name="keyInfo">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="version" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" form="unqualified" name="public" type="xs:boolean" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Possible Attributes Binding Support

attributeFormDefault

The .NET Framework recognizes the default value, unqualified. The Xsd.exe tool translates a qualified value on a per-field basis, by setting the Form property of each non-overridden field's XmlAttributeAttribute accordingly.

See the preceding section, AttributeFormDefault Attribute.

blockDefault

The blockDefault attribute of the <schema> element provides a default value for the block attributes of the <complexType> and <element> elements.

The Xsd.exe tool ignores the blockDefault attribute of the <schema> element, as well as the block attribute in its respective elements.

elementFormDefault

The .NET Framework effectively uses a different default value, qualified. The Xsd.exe tool always generates a <schema> element with elementFormDefault="qualified". The tool translates an unqualified value on a per-field basis, by setting the Form property of each non-overridden field's XmlElementAttribute accordingly.

See the preceding section, ElementFormDefault Attribute.

finalDefault

The finalDefault attribute of the <schema> element provides a default value for the final attributes of certain elements.

The Xsd.exe tool ignores the finalDefault attribute of the <schema> element, as well as the final attribute in its respective elements.

id

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

xml:lang

Xsd.exe ignores the lang attribute, from the namespace http://www.w3.org/XML/1198, when used in the root <schema> element.

targetNamespace

The Xsd.exe tool relates the targetNamespace attribute of the <schema> element to Namespace properties of various XML-related attributes. Which attributes are used depends on the direction of the translation.

See the preceding section, TargetNamespace Attribute.

version

The version attribute is available for documentation. The Xsd.exe tool ignores the attribute.

Possible parent elements: none (root element)

Possible child elements: <annotation>, <attribute>, <attributeGroup>, <complexType>, <element>, <group>, <import>, <include>, <notation>, <redefine>, <simpleType>

See Also

Reference

XmlSchema