Share via


Form Attribute Binding Support

The .NET Framework provides binding support for the form attribute.

The Xsd.exe tool equates the form XML attribute with the Form property of the XmlAttribute or XmlElement attributes, although the .NET Framework's XML serialization code recognizes a different default value, qualified, for elements.

Explanation

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 specify whether to namespace-qualify a particular local element or attribute, the form attribute is added to a local <element> or <attribute> declaration. The possible values are qualified and unqualified.

The form attribute's default value is inherited from either the elementFormDefault or the attributeFormDefault attribute of the root <schema> element. For both of those attributes, the default value is unqualified.

The .NET Framework does not have a way of specifying form on a global or schema-wide level within a class definition. Instead, when generating an XML Schema document from a set of classes, Xsd.exe always produces an XML schema that has the following characteristics:

  • Does not specify attributeFormDefault, reverting to the default unqualified.

  • Specifies elementFormDefault="qualified", effectively using a different default value than does the XML Schema.

Xsd.exe responds to alternate values for the attributeFormDefault="qualified" and elementFormDefault="unqualified" default values by setting form on a per-field basis. (For more details, see attributeFormDefault Attribute and elementFormDefault Attribute.)

Given these constraints, Xsd.exe generates source code from the form attribute as shown in the following table.

form attribute

form="qualified"

form="unqualified"

<attribute> element

The XmlAttribute attribute declaration is passed a Form=XmlSchemaForm.Qualified parameter.

The XmlAttribute attribute declaration for the <attribute> field is passed no form parameter.

<element> element

No XmlElement attribute is applied, unless to pass an unrelated parameter.

The XmlElement attribute declaration for the <element> field is passed a parameter, Form=XmlSchemaForm.Unqualified.

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

  • XmlSchemaForm.Qualified

  • XmlSchemaForm.Unqualified

  • XmlSchemaForm.None: the default

Form is specified only in the source code if it is set to the non-default value in the original XML Schema document, according to the .NET Framework defaults as opposed to the XML Schema defaults. Because the form attribute overrides the attributeFormDefault and elementFormDefault attributes, a schema that sets form to the non-default at the <schema> level and then sets it to the default at the <attribute> or <element> level produces source that does not specify form. In terms of conforming instance documents, the meaning is still the same.

Example

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="ComplexInstance" type="MyComplexType"/>
  <xsd:complexType name="MyComplexType">
    <xsd:sequence>
      <xsd:element name="elementQ" type="xsd:decimal" form="qualified" />
      <xsd:element name="elementU" type="xsd:Date" form="unqualified" />
    </xsd:sequence>
    <xsd:attribute name="attributeQ" type="xsd:string"  use="required" form="qualified"/>
    <xsd:attribute name="attributeU" type="xsd:boolean" use="required" form="unqualified"/>
  </xsd:complexType>
</xsd:schema>

C# class generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("ComplexInstance", Namespace="http://example.org/", IsNullable=false)]
 public class MyComplexType {
        
     public System.Decimal elementQ;
        
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string elementU;
        
    [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
    public string attributeQ;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool attributeU;
}

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="ComplexInstance" type="tns:MyComplexType" />
  <xs:complexType name="MyComplexType">
    <xs:sequence>
      <xs:element minOccurs="1" maxOccurs="1" name="elementQ" type="xs:decimal" />
      <xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="elementU" type="xs:string" />
    </xs:sequence>
    <xs:attribute form="qualified" name="attributeQ" type="xs:string" />
    <xs:attribute name="attributeU" type="xs:boolean" use="required" />
  </xs:complexType>
</xs:schema>

Possible containing elements: <attribute>, <element>

See Also

Reference

XmlSchemaAttribute.Form
XmlSchemaElement.Form