從 XML 結構描述 (XSD) 衍生資料集關聯式結構

這個章節提供如何從 XML 結構描述定義語言 (XSD) 結構描述文件來建置 DataSet 關聯式結構描述的概觀。 一般而言,針對結構描述元素的每個 complexType 子元素,都會在 DataSet 產生一個資料表。 資料表結構由複雜型別的定義來決定。 DataSet 針對結構描述的最上層元素建立資料表。 不過,只有當 complexType 元素巢狀於另一個 complexType 元素時,才會為最上層 complexType 元素建立資料表,在此情況下,巢狀 complexType 元素會對應至 DataTable 內的 DataSet

如需 XSD 的詳細資訊,請參閱全球資訊網協會 (W3C) 的 XML 結構描述第零部:入門建議事項 (英文)XML 結構描述第一部:結構建議事項 (英文),以及 XML 結構描述第二部:資料型別建議事項 (英文)

下列範例會示範 XML 結構描述,其中 customersMyDataSet 元素 (此元素為 DataSet 元素) 的子元素。

<xs:schema id="SomeID"
            xmlns=""
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
   <xs:element name="MyDataSet" msdata:IsDataSet="true">  
     <xs:complexType>  
       <xs:choice maxOccurs="unbounded">  
         <xs:element name="customers" >
           <xs:complexType >  
             <xs:sequence>  
               <xs:element name="CustomerID" type="xs:integer"
                            minOccurs="0" />  
               <xs:element name="CompanyName" type="xs:string"
                            minOccurs="0" />  
               <xs:element name="Phone" type="xs:string" />  
             </xs:sequence>  
           </xs:complexType>  
          </xs:element>  
       </xs:choice>  
     </xs:complexType>  
   </xs:element>  
 </xs:schema>  

上述範例中,customers 項目為複雜型別項目。 因此,複雜型別定義會經過剖析,而對應處理序則會產生下列表格。

Customers (CustomerID, CompanyName, Phone)  

資料表內每個資料行的資料型別都衍生自對應項目的 XML 結構描述型別或指定屬性。

注意

如果 customers 元素為簡單 XML 結構描述資料型別 (例如 integer),則不會產生資料表。 因為資料表僅會針對複雜型別的最上層項目產生。

在下列 XML 結構描述,Schema 元素具有兩個元素子系:InStateCustomersOutOfStateCustomers

<xs:schema id="SomeID"
            xmlns=""
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
   <xs:element name="InStateCustomers" type="customerType" />  
   <xs:element name="OutOfStateCustomers" type="customerType" />  
    <xs:complexType name="customerType" >  
  
     </xs:complexType>  
  
   <xs:element name="MyDataSet" msdata:IsDataSet="true">  
     <xs:complexType>  
       <xs:choice maxOccurs="unbounded">  
         <xs:element ref="customers" />  
       </xs:choice>  
     </xs:complexType>  
   </xs:element>  
 </xs:schema>  

InStateCustomersOutOfStateCustomers 子項目均為複雜型別項目 (customerType)。 所以,對應處理會在 DataSet 產生下列兩個相同的表格。

InStateCustomers (CustomerID, CompanyName, Phone)  
OutOfStateCustomers (CustomerID, CompanyName, Phone)  

本節內容

將 XML 結構描述 (XSD) 條件約束對應至資料集條件約束
說明用來在 DataSet 建立唯一外部索引鍵限制式的 XML 結構描述元素。

從 XML 結構描述 (XSD) 產生資料集關聯
說明用來在 DataSet 的資料表資料行間建立關聯的 XML 結構描述元素。

XML 結構描述條件約束和關聯性
說明使用 XML 結構描述元素在 DataSet 建立限制式時,如何隱含建立關聯。

在資料集中使用 XML
說明如何在 DataSet 將關聯式結構載入並保存為 XML 資料。

另請參閱