Freigeben über


Generating DataSet Relational Structure from XML Schema (XSD) 

This section provides an overview of how the relational schema of a DataSet is built from an XML Schema definition language (XSD) schema document. In general, for each complexType child element of a schema element, a table is generated in the DataSet. The table structure is determined by the definition of the complex type. Tables are created in the DataSet for top-level elements in the schema. However, a table is only created for a top-level complexType element when the complexType element is nested inside another complexType element, in which case the nested complexType element is mapped to a DataTable within the DataSet.

For more information about the XSD, see the World Wide Web Consortium (W3C) XML Schema Part 0: Primer Recommendation, the XML Schema Part 1: Structures Recommendation, and the XML Schema Part 2: Datatypes Recommendation, located at https://www.w3.org/.

The following example demonstrates an XML Schema where customers is the child element of the MyDataSet element, which is a DataSet element.

 <xs:schema id="SomeID" 
              
             xmlns:xs="https://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>

In the preceding example, the element customers is a complex type element. Therefore, the complex type definition is parsed, and the mapping process creates the following table.

Customers (CustomerID , CompanyName, Phone)

The data type of each column in the table is derived from the XML Schema type of the corresponding element or attribute specified.

Note

If the element customers is of a simple XML Schema data type such as integer, no table is generated. Tables are only created for the top-level elements that are complex types.

In the following XML Schema, the Schema element has two element children, InStateCustomers and OutOfStateCustomers.

 <xs:schema id="SomeID" 
              
             xmlns:xs="https://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>

Both the InStateCustomers and the OutOfStateCustomers child elements are complex type elements (customerType). Therefore, the mapping process generates the following two identical tables in the DataSet.

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

In This Section

  • Using XML in a DataSet
    Describes how to load and persist the relational structure and data in a DataSet as XML data.