List Element Binding SupportĀ 

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

The .NET Framework provides accurate bindings for XML attribute declarations with list types, but not for element declarations.

Explanation

The <list> construct is used to define a simple type whose possible values are a white-space delimited series of values from another simple type. The types that serve as building blocks are called atomic types because they cannot be divided into anything meaningful. Any simple type, except for a list or union type, is atomic.

The following example shows the creation of a list type where the values can be white-space delimited decimals:

<xsd:simpleType name="numbersList">
  <xsd:list itemType="xsd:Decimal"/>
</xsd:simpleType>

The .NET Framework provides bindings for the <list> element when it defines attribute types, but not element types.

When generating source code from an XML Schema document, if Xsd.exe encounters an element with a list type, it produces a field whose type is the .NET Framework type corresponding to the list's constituent type, which is the value of the itemTypeattribute. For string-based types, this translation is technically correct because a string of strings is still a string. For list types based on other simple types, such as the decimal-based type in the preceding example, the translation is erroneous.

If Xsd.exe encounters an attribute with a list type, it produces a field whose type is an array of the .NET Framework type corresponding to the value of the itemType attribute. The field appears with the attribute XmlAttributeAttribute. Following is an example of a generated field:

[System.Xml.Serialization.XmlAttributeAttribute()]
public System.Decimal[] numbers;

Likewise, when generating an XML Schema document from a set of classes, Xsd.exe looks for the following conditions in a field or property to produce a list type:

  • The field or property's type is an array of a type that can map to a simple type.

  • The field or property appears with an XmlAttribute attribute.

This combination can be expressed only as a <list> type, because an XML attribute must have a simple type and an array would otherwise be bound to a complex type (with a maxOccurs attribute set to unbounded).

Example

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="alphabet" type="AlphabetType"/>

    <xsd:complexType name="AlphabetType">
        <xsd:sequence>
            <xsd:element name="language" type="xsd:string"/>
            <xsd:element name="count" type="xsd:decimal"/>
        </xsd:sequence>
        <xsd:attribute name="letters">
            <xsd:simpleType>
                <xsd:list itemType="Character"/>
            </xsd:simpleType>
        </xsd:attribute>
    </xsd:complexType>

    <xsd:simpleType name="Character">
        <xsd:restriction base="xsd:string">
            <xsd:length value="1"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

C# class generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("alphabet", Namespace="http://example.org/", IsNullable=false)]
public class AlphabetType {
        
    public string language;
        
    public System.Decimal count;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string[] letters;
}

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

<xs:complexType name="AlphabetType">
  <xs:sequence>
    <xs:element minOccurs="0" maxOccurs="1" name="language" type="xs:string" />
    <xs:element minOccurs="1" maxOccurs="1" name="count" type="xs:decimal" />
  </xs:sequence>
  <xs:attribute name="letters">
    <xs:simpleType>
      <xs:list itemType="xs:string" />
    </xs:simpleType>
  </xs:attribute>
</xs:complexType>
Possible Attributes Binding Support

id

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

itemType

When a list type is used as an XML attribute (but not as an element), the Xsd.exe tool produces a field that is an array of the .NET Framework type corresponding to the value of the itemType attribute.

Possible parent elements: <simpleType>

Possible child elements: <annotation>, <simpleType>

See Also

Reference

XmlSchemaSimpleTypeList