Share via


Group Element Binding SupportĀ 

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

When generating source code from an XML Schema document, Xsd.exe expands each <group> reference directly into the class corresponding to the <complexType> definition that contains the reference.

Explanation

The <group> element allows the schema designer to globally define a group of elements and then reuse that group in any number of complex types, via references.

The .NET Framework does not have an idiom for expressing element groups in code. Instead, when generating source code from an XML Schema document, Xsd.exe expands each <group> reference via the ref attribute directly into the class corresponding to the <complexType> definition that contains the reference. For each element, a public field is produced.

A developer wishing to avoid defining the same group of element-binding fields or properties directly in multiple classes could manually create a base class and have the classes representing XML Schema complex types inherit from that base class.

The XML Schema definition language lets a <group> reference appear with a maxOccurs attribute having a value greater than 1. To properly import schemas with multiply occurring groups, use the /order command line option on Xsd.exe. (When generating source code from such an XML schema, by default, Xsd.exe incorrectly generates an array for each element in the group. With the /order option enabled, one array will be generated for all members of the group, with multiple XmlElementAttribute attributes, one for each group member.)

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:group name="version">
      <xsd:sequence>
        <xsd:element name="changeNumber" type="xsd:int"/>
        <xsd:element name="instanceId" type="xsd:string"/>
      </xsd:sequence>
    </xsd:group>
    <xsd:complexType name="keyInfo">
      <xsd:sequence>
        <xsd:element name="key" type="xsd:string"/>
        <xsd:group ref="version" />
      </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 key;
        
    public int changeNumber;
        
    public string instanceId;
}

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

<xs:complexType name="keyInfo">
  <xs:sequence>
    <xs:element minOccurs="0" maxOccurs="1" name="key" type="xs:string" />
    <xs:element minOccurs="1" maxOccurs="1" name="changeNumber" type="xs:int" />
    <xs:element minOccurs="0" maxOccurs="1" name="instanceId" type="xs:string" />
  </xs:sequence>
</xs:complexType>
Possible Attributes Binding Support

id

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

maxOccurs

When generating source code from an XML Schema document, with groups having many maxOccurs greater than1, use the /order command line switch.

See the MaxOccurs Attribute Binding Support attribute.

minOccurs

When generating source code from an XML Schema document, Xsd.exe ignores the minOccurs attribute if applied to the <group> element.

See the MinOccurs Attribute Binding Support attribute.

name

Since the Xsd.exe utility anonymously expands the contents of the <attributeGroup> element, the attribute group name is ignored.

See the Name Attribute Binding Support attribute.

ref

The .NET Framework does not have an idiom for expressing groups in code. Instead, when generating source code from an XML Schema document, Xsd.exe directly expands each ref attribute's <group> reference to a globally declared group into the class corresponding to the <complexType> definition that contains the reference. For each element from a <group>, a public field is produced.

Possible parent elements: <choice>, <complexType>, <extension>, <redefine>, <restriction>, <schema>, <sequence>

Possible child elements: <all>, <annotation>, <choice>, <sequence>

See Also

Reference

XmlSchemaGroup
XmlSchemaGroupRef