共用方式為


ComplexType 項目繫結支援

.NET Framework 會提供 <complexType> 項目的繫結支援。

Xsd.exe 工具等同於具有 .NET Framework 型別的 XML 結構描述複雜型別,而 .NET Framework 型別的公用欄位或屬性代表複雜型別的內容。

說明

Xsd.exe 工具等同於具有 .NET Framework 型別的 XML 結構描述複雜型別,而 .NET Framework 型別的公用欄位代表複雜型別的內容。

Abstract 屬性

將複雜型別宣告為抽象 (abstract="true"),可以確保一致的 XML 文件中只會出現衍生型別的執行個體,而不會出現抽象基底型別 (Base Type) 的執行個體。

Xsd.exe 等同於具有抽象類別的抽象複雜型別。程式碼和 XSD 文件之間可以進行雙向轉換。

如果未使用 abstract 關鍵字,抽象類別會被視為如同非抽象的基底類別。子類別會擴充基底類別。Xsd.exe 為每個子代類別在該抽象類別套用一個 System.Xml.Serialization.XmlIncludeAttribute。每個 XmlInclude 屬性都會指定子代類別的型別。

範例:Abstract 屬性

下列程式碼範例示範如何使用 abstract 屬性搭配 <complexType> 項目。

輸入 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="FamilyNode" type="FamilyNodeType" />
  <xsd:complexType name="FamilyNodeType">
    <xsd:sequence>
      <xsd:element name="Code" type="xsd:string" />
      <xsd:element name="Parent" type="Parent" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Daughter">
    <xsd:complexContent>
      <xsd:extension base="Parent">
        <xsd:sequence>
          <xsd:element name="Date" type="xsd:dateTime" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="Son">
    <xsd:complexContent>
      <xsd:extension base="Parent">
        <xsd:sequence>
          <xsd:element name="Info" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="Parent" abstract="true">
    <xsd:sequence>
      <xsd:element name="Text" type="xsd:normalizedString" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Grandchild">
    <xsd:complexContent>
      <xsd:extension base="Daughter">
        <xsd:attribute name="Parent" type="xsd:normalizedString" />
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

由前面的 XML 結構描述文件所產生的 C# 類別:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("FamilyNode", Namespace="http://example.org/", IsNullable=false)]
public class FamilyNodeType {
        
    public string Code;
        
    public Parent Parent;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Son))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Daughter))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public abstract class Parent {
    [System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
    public string Text;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Son : Parent {        
    public string Info;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public class Daughter : Parent {
        
    public System.DateTime Date;
        
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool DateSpecified;
}
    
 [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Grandchild : Daughter {        
    [System.Xml.Serialization.XmlAttributeAttribute("Parent", DataType="normalizedString")]
    public string Parent1;
}

由前面的 C# 原始程式碼所編譯的類別而產生的 XML 結構描述,會有效地與原始的 XML 結構描述一樣。

可能的屬性 繫結支援

abstract

Xsd.exe 公用程式等同於具有抽象類別的抽象複雜型別,此型別由 abstract="true" 辨識。對於繼承自抽象類別的每個類別,Xsd.exe 都會將 System.Xml.Serialization.XmlIncludeAttribute 套用至抽象類別,並指定子代類別的型別。

請參閱前面Abstract Attribute一節。

block

block 屬性套用至資料型別,可以避免衍生型別取代已指定的原始型別。

Xsd.exe 工具會忽略 block 屬性和 Schema 項目繫結支援 項目的 blockDefault 屬性。

final

final 屬性可以套用至資料型別,使系統不衍生這個型別。

Xsd.exe 會忽略 <schema> 項目的 final 屬性和 finalDefault 屬性。

id

Xsd.exe 公用程式忽略試圖提供唯一識別項的 id 屬性。相反地,Xsd.exe 會辨認 name 屬性。

mixed

請參閱 mixed 屬性。

name

name 屬性的值會變成 Xsd.exe 從複雜型別產生之 .NET Framework 型別的名稱。

未嘗試轉換大小寫,以遵守程式碼撰寫慣例。例如,如果 <complexType> 項目的名稱屬性具有值 testInfo,則結果類別會命名為 testInfo,而不是首字大寫的 TestInfo。如果名稱與保留的關鍵字衝突,則所產生的名稱前面會加上 @ 符號。

當 Xsd.exe 從類別產生 <complexType> 定義時,會使用類別名稱當做 name 屬性的值。透過 TypeName 屬性 (Property) 可以提供替代名稱 name 屬性 (Attribute) 值。

請參閱 Name 屬性繫結支援 屬性。

可能的父項目:<element><redefine><schema>

可能的子項目:<all><annotation><anyAttribute><attribute><attributeGroup><choice><complexContent><group><sequence><simpleContent>

請參閱

參考

XmlSchemaComplexType

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.