共用方式為


Element 項目繫結支援

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

說明

XML 結構描述規格指出項目可以在複雜型別定義中區域宣告,或是全域宣告為根 <schema> 項目的子系。如果項目為全域宣告,則可以透過 ref 屬性從一或多個複雜型別定義加以參考。

下列程式碼範例顯示區域宣告的項目:

<xsd:complexType name="UnitType">
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>

下列程式碼範例顯示已全域宣告且加以參考的同一個項目:

<xsd:element name="name" type="xsd:string"/>
<xsd:complexType name="UnitType">
  <xsd:sequence>
    <xsd:element ref="name"/>
  </xsd:sequence>
</xsd:complexType>

從 XML 結構描述複雜型別產生 .NET Framework 型別時,除非在結構描述目標命名空間之外的命名空間中宣告全域項目,否則 Xsd.exe 不會區別區域宣告的項目和對全域宣告之項目的參考。

相同命名空間中的參考

因為 Xsd.exe 在相同命名空間內不會進行區別,所以自 XML 結構描述轉譯至類別再反向轉譯的過程,就會建立可取代全域項目之任何參考的區域項目。

如果所參考之全域項目的資料型別已自動繫結至 .NET Framework 型別,Xsd.exe 就不會產生對應到資料型別的新型別。因此,在雙向轉譯所產生的 XML 結構描述中,將不再參考也不會出現全域項目宣告。

Note注意:

所有的內建 XML 結構描述資料型別都會自動繫結至 .NET Framework 型別。此外,結構描述定義的簡單型別會繫結至 .NET Framework 型別,而非自訂型別,但是有一個例外狀況。此單一例外狀況與字串繫結型別的列舉型別 (Enumeration) 有關;在 <enumeration> 項目的文件中會提供詳細說明。

Note注意:

如需結構描述定義簡單型別之支援的詳細資訊,請參閱 <restriction> 項目或任何表示限制 Facet 的項目。

參考其他命名空間

如果參考的全域宣告屬於不同的命名空間,則 Xsd.exe 會使用套用至所產生欄位的 XmlElementAttribute 屬性 (Attribute) 的 Namespace 屬性 (Property) 指定命名空間。對於該特定項目,透過 Namespace 屬性 (Property) 指定的命名空間會使用 XmlTypeAttribute 屬性 (Attribute) 和選用的 XmlRootAttribute,覆寫在類別層級指定的命名空間。範例如下:

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem")]
public string Text;

使用 <import> 項目將其他命名空間匯入 XML 結構描述定義。

建立全域項目宣告

從組件中的一組類別產生 XML 結構描述文件時,Xsd.exe 會針對每個從組件中定義型別所產生的 <complexType><simpleType> 定義,產生全域 <element> 宣告。

第一個範例顯示當定義全域項目的目標命名空間含有該項目的參考時,Xsd.exe 會如何處理這個全域項目。

輸入 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="Text" type="xsd:normalizedString"/>
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

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

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

從前面 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="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" name="Text" type="xs:normalizedString" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

在前面產生的 XML 結構描述中,本來是全域宣告的 Text 項目已經變成區域項目。

第二個範例顯示當全域項目定義於不同的命名空間中時,Xsd.exe 會如何處理該項目的參考。這個範例會使用 <import> 項目,匯入位於不同的 XSD 檔中的第二個命名空間(<import> 項目的 schemaLocation 屬性不是用來指定所匯入 .xsd 檔的位置,而是針對 Xsd.exe 將檔案指定為其他命令列引數)。

用來當做輸入的最上層 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
      xmlns="http://example.org/" targetNamespace="http://example.org/" xmlns:elem="http://example.org/elem">
  <xsd:import  namespace="http://example.org/elem" />
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="elem:Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

用來當做輸入的匯入 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
      xmlns="http://example.org/elem" targetNamespace="http://example.org/elem">
  <xsd:element name="Text" type="xsd:normalizedString"/>
</xsd:schema>

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

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem", DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

從前面 C# 來源編譯之組件產生的最上層 XML 結構描述文件:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://example.org/elem" />
  <xs:element name="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" xmlns:q1="http://example.org/elem" ref="q1:Text" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

匯入的 XML 結構描述文件會從前面 C# 原始檔所編譯的組件而產生:

<xs:schema xmlns:tns="http://example.org/elem" elementFormDefault="qualified" targetNamespace="http://example.org/elem" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Text" type="xs:normalizedString" />
</xs:schema>

Abstract 屬性

全域項目宣告可以具有 abstract 屬性,區域項目則否。

宣告項目為 Abstract (abstract="true") 以便透過替代群組強制執行替代作業;如果項目並非 Abstract,則替代作業為選擇性的作業。

要被替代的全域項目稱為標頭項目。當區域項目宣告透過 ref 屬性參考標頭項目時,您可以使用任何全域替代項目加以取代。如需詳細資訊,請參閱 substitutionGroup 屬性。

如果替代項目屬於衍生的資料型別,並且標頭項目本身宣告為 Abstract (而非將項目型別的 <complexType> 定義宣告為 Abstract),則即使無法在發生替代作業的位置上使用基底型別 (Base Type),您還是可以在 XML 執行個體文件中的其他位置使用基底型別。

從 XML 結構描述文件產生原始程式碼時,Xsd.exe 會產生對應到標頭項目之資料型別的型別。該型別包含名為 Item 的欄位。Xsd.exe 會為每個替代作業在該欄位套用一個 XmlElementAttribute。每個屬性都會辨識名稱 (如果替代項目具有衍生資料型別,則會辨識型別)。如果替代項目使用的資料型別與標頭項目相同,那麼也必須套用 XmlChoiceIdentifierAttribute。如需詳細資訊,請參閱 <choice> 項目。

下列程式碼範例顯示從 Abstract 標頭項目之參考所產生的範例欄位 Item

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

如果您使用的替代項目屬於衍生資料型別,則只有當已指派其中一個衍生型別的執行個體至該欄位時,XML 序列化 (Serialization) 才能運作。如果是指派對應到標頭項目的基底型別執行個體,XmlSerializer 類別就會失敗。

在反向轉譯中,Xsd.exe 會產生 <choice> 項目,其中包含每個替代項目的 <element> 項目,但是沒有代表標頭項目的 <element>。項目是在複雜型別定義中獨立定義的,並未採用 ref 參考。

Note注意:

如果區域 <element> 宣告中包含的 ref 屬性指定 Abstract 標頭項目,則 Xsd.exe 會忽略 maxOccurs 屬性。

Note注意:

請考慮下列型別 MyBaseType 之標頭項目的宣告:

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />
Note注意:

針對前面的宣告,Xsd.exe 仍會產生下列欄位 (不計屬性):

public MyBaseType Item;
Note注意:

如果輸入 XML 結構描述中的所有全域宣告項目都是 Abstract,Xsd.exe 就無法產生類別。

範例:Abstract 屬性

輸入 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

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

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

編譯自前面 C# 原始檔而得之組件所產生的 XML 結構描述文件:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema> 

SubstitutionGroup 屬性

只有當替代項目宣告為 Abstract 時,Xsd.exe 工具才會辨識替代群組 (請參閱前面Abstract Attribute一節)。然後,在反向轉譯上,Xsd.exe 會產生 <choice> 項目,其中包含每個替代作業的 <element> 項目。

SubstitutionGroup 屬性:Background

substitutionGroup 屬性可以與全域宣告項目一起出現,讓該項目可以取代 XML 執行個體文件中的其他全域宣告項目。屬性值即為被替代的項目的名稱,這個項目叫做標頭。實際的替代作業是在複雜型別定義的 <element> 項目中進行,其中會透過 ref 屬性參考標頭項目。任何使用 substitutionGroup 屬性宣告的項目所屬之資料型別必須與標頭項目相同,或者屬於衍生型別。

替代群組可用來允許不同名稱、且具有相同基底型別的不同型別 (選擇性地) 之項目,出現在執行個體文件的指定位置中。

不支援巢狀替代群組。也就是說,如果某個項目在一個替代群組中當做標頭項目,那麼就不能成為另一個替代群組內的替代項目。例如:

<xs:element name="Animal" abstract="true"/>
<xs:element name="Mammal" abstract="true" 
    substitutionGroup="tns:Animal"/>
<xs:element name="Dog" type="xs:string" 
    substitutionGroup="tns:Mammal"/>

這是不支援的,因為 Mammal 同時做為 Dog 的標頭,也是 Animal 的替代項目。如果遇到此類結構描述,Xsd.exe 將不會為 Mammal 產生 XmlElementAttribute,而 Dog 將不會位於 Animal 項目所預期的位置。不過,您可以手動加入讓這種情形成立,如下列範例所示:

public class PetShop 
{
    private object Animal;
    
    // Apply the XmlElementAttribute to the generated property.
    [System.Xml.Serialization.XmlElementAttribute("Mammal"), 
     System.Xml.Serialization.XmlElementAttribute("Dog")]
    public object Animal {
        get {
            return this.Animal;
        }
        set {
            this.Animal = value;
        }
    }
}

SubstitutionGroup 屬性:Concrete 標頭項目

如果標頭項目不是 Abstract,Xsd.exe 會採用每個標頭的 ref 參考,並建立對應至標頭 XML 資料型別之型別的欄位 (或是該型別的陣列,視所參考項目之 maxOccurs 屬性的值而定)。替代群組的任何參考均已遺失。

當衍生型別的物件可被指派至該欄位、且 XmlSerializer 類別會序列化該物件時,這個行為會在具有衍生型別的替代作業之外發生。不會使用替代項目名稱。

SubstitutionGroup 屬性:Abstract 標頭項目

如果標頭項目是 Abstract,Xsd.exe 就會產生對應到標頭資料型別之型別的欄位。欄位會命名為 Item。Xsd.exe 會為每個替代作業在該欄位套用一個 XmlElementAttribute。每個屬性都會辨識名稱 (如果替代項目具有衍生資料型別,則會辨識型別)。如果替代項目使用的資料型別與標頭項目相同,那麼也必須套用 XmlChoiceIdentifierAttribute。請參閱 <choice> 項目。

下列是從 Abstract 標頭項目的參考所產生的範例欄位:

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

如果正在使用衍生的型別,則唯有將其中一個衍生型別的執行個體指派至該欄位時,XML 序列化才會運作。如果已指派對應到標頭項目的基底型別執行個體,XmlSerializer 就會失敗。

在反向轉譯中,Xsd.exe 會從此建構中產生 <choice> 項目,其中包含每個替代項目而非標頭項目的 <element> 項目。項目是在複雜型別定義中獨立定義的,並未採用 ref 參考。

maxOccurs 屬性:針對區域 <element> 宣告包含指定 Abstract 標頭項目的 ref 屬性,Xsd.exe 會忽略 maxOccurs 屬性。

請考慮下列宣告,它會參考型別 MyBaseType 的標頭項目:

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />

針對前面的宣告,Xsd.exe 仍會產生下列欄位 (不計屬性):

public MyBaseType Item;

範例:SubstitutionGroup 屬性

輸入 XML 結構描述文件:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

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

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

編譯自前面 C# 原始檔而得之組件所產生的 XML 結構描述文件:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
可能的屬性 繫結支援

abstract

Xsd.exe 公用程式會使用 Abstract <element> 宣告繫結替代群組,但是會在反向轉譯上產生 <choice> 項目。

請參閱前面「Abstract 屬性」一節。

block

block 屬性可以用來避免使用項目宣告做為替代群組標頭 (亦即群組成員所替代的項目)。

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

default

如果執行個體文件中的項目是空白的,default 屬性就會提供所要使用的預設值。如果項目根本沒有出現,這個屬性就不會有值。

從 XML 結構描述文件產生原始程式碼時,Xsd.exe 工具會接受每個對應至具有預設值之項目的欄位,並套用 System.ComponentModel.DefaultValueAttribute,以傳遞預設值當做引數。此外,Xsd.exe 會將欄位靜態初始化為預設值,如下列範例所示:

[System.ComponentModel.DefaultValueAttribute(-1)]
public int age = -1;

從具有 .NET Framework 實值型別 (Value Type) 的類別成員產生 <element> 項目時,Xsd.exe 會使用 DefaultValue 屬性做為設定 minOccurs 屬性的輸入。如果 Xsd.exe 遇到已套用至這類成員的 DefaultValue 屬性,就會針對項目的 minOccurs 屬性產生 0 值。這表示此項目不需要出現在有效的 XML 執行個體文件中。

在陣列 (項目的 maxOccurs 大於 1) 上會忽略預設的屬性。

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

final

final 屬性可以用來避免使用項目宣告做為替代群組標頭 (亦即群組成員所替代的項目)。

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

fixed

固定屬性會為執行個體文件中的項目提供固定值。從 XML 結構描述文件產生原始程式碼時,Xsd.exe 會使用對應至包含固定值之項目的每個欄位,並為欄位產生靜態初始設定式,如下列範例所示:

public int age = -1;

在陣列 (項目的 maxOccurs 大於 1) 上會忽略固定的屬性。

form

Xsd.exe 工具會將 <element> 項目的 form XML 屬性 (Attribute) 視做等同於 XmlElementAttributeForm 屬性 (Property)。.NET Framework 的 XML 序列化基礎結構會辨識不同的預設值 qualified

如果 XML 結構描述中的 <element> 宣告指定 form="unqualified",則 Xsd.exe 會針對相應的欄位產生 XmlElement 屬性,並傳遞參數 Form=XmlSchemaForm.Unqualified 給這個屬性。

請參閱 form 屬性。

id

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

maxOccurs

從 XSD 文件產生類別時,Xsd.exe 會根據下列可能值解譯 <element> 項目中的 maxOccurs 屬性:

  • 1:Xsd.exe 會產生欄位,這個欄位的型別會對應至項目的資料型別。

  • 0:Xsd.exe 無法處理為 0 的值,而會將此值視為預設值 1

  • unbounded:Xsd.exe 會產生一個欄位,這個欄位是一個型別對應至項目資料型別的陣列。

  • 對於大於 1 的整數:如同和 unbounded 值的處理方式一般,Xsd.exe 會產生一個欄位,這個欄位是一個型別對應至項目資料型別的陣列。您可以使用 XmlValidatingReader 類別,比對 SOM 所表示的 XML 結構描述文件,驗證 XML 文件,藉此強制值大於 1。

從組件中的一組類別產生 XML 結構描述文件時,Xsd.exe 會回復之前執行的轉換,從單一執行個體產生值為 1maxOccurs,並從陣列產生值為 unboundedmaxOccurs

Xsd.exe 會將值為 unboundedmaxOccurs 繫結至陣列,而將值為 1maxOccurs 繫結至陣列中所指定的父項目 (如果有的話)。如需如何使用 maxOccurs 項目進行陣列繫結 (包括指定其他父項目) 的詳細資訊,請參閱 MaxOccurs 屬性繫結支援 屬性。

minOccurs

至於 <element> 項目,Xsd.exe 只有在 MaxOccurs 屬性繫結支援 屬性的值不是指定陣列欄位時,才會檢查 minOccurs 屬性的值。然後會根據各種因素解譯 (或產生) 值,首先需視對應的欄位是否具備參考或實值型別而定。

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

name

從 XSD 文件產生原始程式碼時,name 屬性的值會提供公用類別欄位的名稱,此欄位代表該項目。如果 <element> 項目含有匿名的 <complexType> 定義,名稱就會變成對應至複雜型別之類別的名稱。

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

當 Xsd.exe 從公用類別欄位產生 <element> 宣告時,會使用欄位名稱當做 name 屬性的值。透過下列屬性 (Attribute) 的屬性 (Property),可以提供替代名稱 name 屬性 (Attribute) 值。

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

nillable

Xsd.exe 工具會將 nillable 屬性 (Attribute) 視做等同於套用至參考型別之特定 XML 序列化相關屬性 (Attribute) 的 IsNullable 屬性 (Property)。對實值型別而言,truenillable 值會產生可為 null 的型別。

請參閱 nillable 屬性。nillable 屬性只會出現在 <element> 項目中。

ref

從 XML 結構描述複雜型別產生 .NET Framework 型別時,除非在結構描述目標命名空間之外的命名空間中宣告全域項目,否則 Xsd.exe 不會區別區域宣告的項目和對全域宣告之項目的參考。

請參閱「相同命名空間中的參考」和「參考其他命名空間」等節。

substitutionGroup

只有當替代項目宣告為 Abstract 時,Xsd.exe 工具才會辨識替代群組。然後,在反向轉譯上,Xsd.exe 會產生 <choice> 項目,其中包含每個替代作業的 <element> 項目。

請參閱前面「SubstitutionGroup 屬性」一節。

type

如果使用 <element><attribute> 宣告的 type 屬性參考資料型別,則 Xsd.exe 工具會使該資料型別與 .NET Framework 型別產生關聯。

除非可以追蹤 XML 結構描述資料型別,返回透過 type 屬性參考該資料型別 (可能是其他資料型別) 的全域項目宣告,否則 Xsd.exe 不會針對該資料型別產生 .NET Framework 型別。

可能的父項目:<all><choice><schema><sequence>

可能的子項目:<annotation><complexType><key><keyref><simpleType><unique>

請參閱

參考

XmlSchemaElement

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.