AttributeGroup 要素のバインディング サポート

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

.NET Framework では、<attributeGroup> 要素のバインディングを一部サポートしています。

XML スキーマ ドキュメントからソース コードを生成するとき、Xsd.exe は各 <attributeGroup> 参照を、その参照を含んでいる <complexType> 定義に対応するクラスに直接展開します。

説明

<attributeGroup> 要素を使用すると、スキーマの設計で、属性のグループをグローバルに定義でき、参照を経由して、任意の数の複合型でそのグループを再利用できます。

.NET Framework には、属性グループをコードで表現する方法がありません。その代わり、XML スキーマ ドキュメントからソース コードを生成するとき、Xsd.exe は ref 属性によって、各 <attributeGroup> 参照を、その参照を含んでいる <complexType> 定義に対応するクラスに直接展開します。各属性に対して、パブリック フィールドが 1 つ生成されます。各パブリック フィールドは、XmlAttributeAttribute 属性 (XmlAttribute という短い名前で表現されることもある) と共に現れます。

属性バインディングのフィールドまたはプロパティの同じグループを、複数のクラスで直接定義しないようにするには、手動で基本クラスを作成し、その基本クラスから継承された XML スキーマ複合型を表すクラスを用意する方法があります。各パブリック フィールドまたはプロパティは、XmlAttribute 属性と共に使用してください。それ以外の場合は、属性ではなく複合型の要素として解釈されます。

Example

入力 XML スキーマ ドキュメント:

<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>

上記の XML スキーマ ドキュメントから生成される C# クラス :

[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;
}

上記の C# ソースからコンパイルされたアセンブリから生成される XML スキーマ複合型 :

<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>

使用可能な属性 バインディング サポート

id

Xsd.exe ユーティリティでは、一意な識別子を提供するための id 属性は無視されます。

name

Xsd.exe ユーティリティでは、<attributeGroup> 要素のコンテンツが匿名で展開されるため、属性のグループ名は無視されます。

Name 属性のバインディング サポート 属性を参照してください。

ref

.NET Framework には、属性グループをコードで表現する方法がありません。その代わり、XML スキーマ ドキュメントからソース コードを生成するとき、Xsd.exe はグローバルに宣言された属性グループへの各 ref 属性の <attributeGroup> 参照を、その参照を含んでいる <complexType> 定義に対応するクラスに直接展開します。<attributeGroup> からの各属性では、System.Xml.Serialization.XmlAttributeAttribute 属性 (XmlAttribute という短い名前で表現されることもある) のあるパブリック フィールドが生成されます。

使用可能な親要素 : <attributeGroup>、<complexType><extension><redefine><restriction><schema>

使用可能な子要素 : <annotation><anyAttribute><attribute>、<attributeGroup>

参照

リファレンス

XmlSchemaAttributeGroup
XmlSchemaAttributeGroupRef