AttributeGroup Element Binding Support 

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

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

Explanation

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

The .NET Framework does not have an idiom for expressing attribute groups in code. Instead, when generating source code from an XML Schema document, Xsd.exe expands each <attributeGroup> reference via the ref attribute directly into the class corresponding to the <complexType> definition that contains the reference. For each attribute, a public field is produced. Each public field appears with the attribute XmlAttributeAttribute, which can also be expressed with the shorthand name XmlAttribute.

Note

The <attributeGroup> element cannot be nested in another <attributeGroup> element. Nested instances will be ignored by the Xsd.exe tool.

A developer wishing to avoid defining the same group of attribute-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. Each public field or property should appear with the XmlAttribute attribute; otherwise, it is interpreted as an element in a complex type, not as an attribute.

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:attributeGroup name="version">
      <xsd:attribute name="changeNumber" type="xsd:int" use="required"/>
      <xsd:attribute name="instanceId" type="xsd:string" use="required"/>
    </xsd:attributeGroup>
    <xsd:complexType name="keyInfo">
      <xsd:sequence>
        <xsd:element name="key" type="xsd:string"/>
      </xsd:sequence>
      <xsd:attributeGroup ref="version" />
    </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;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int changeNumber;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string instanceId;
}

XML Schema complex type 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="key" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="changeNumber" type="xs:int" />
    <xs:attribute name="instanceId" type="xs:string" />
  </xs:complexType>
</xs:schema>
Possible Attributes Binding Support

id

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

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 attribute groups in code. Instead, when generating source code from an XML Schema document, Xsd.exe directly expands each ref attribute's <attributeGroup> reference to a globally declared attribute group into the class corresponding to the <complexType> definition that contains the reference. For each attribute from an <attributeGroup>, a public field is produced with the attribute System.Xml.Serialization.XmlAttributeAttribute, which can also be expressed with the shorthand name XmlAttribute.

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

Possible child elements: <annotation>, <anyAttribute>, <attribute>, <attributeGroup>

See Also

Reference

XmlSchemaAttributeGroup
XmlSchemaAttributeGroupRef