C#高级编程之处理XML-连载十五

时间:2010年04月12日 点击:91

1. 转换关系数据

这看起来非常简单,因为只有一个表。但对于关系数据,例如DataSet中有多个DataTables Relations,该怎么办?其工作方式仍旧是这样。下面对前面的代码进行如下修改(这个版本的代码在ADOSample3)

private void button1_Click(object sender, System.EventArgs e)

{

   //create a dataset

   DataSet ds=new DataSet("XMLProducts");

   //connect to the northwind database and

   //select all of the rows from products table and from suppliers table

   //make sure your connect string matches your server configuration

   SqlConnection conn=new SqlConnection

              (@"server=GLYNNJ_CS\NetSDK;uid=sa;pwd=;database=northwind");

   SqlDataAdapter daProd=new SqlDataAdapter("SELECT * FROM products",conn);

   SqlDataAdapter daSup=new SqlDataAdapter("SELECT * FROM suppliers",conn);

   //Fill DataSet from both SqlAdapters

   daProd.Fill(ds,"products");

   daSup.Fill(ds,"suppliers");

   //Add the relation

   ds.Relations.Add(ds.Tables["suppliers"].Columns["SupplierId"],

                    ds.Tables["products"].Columns["SupplierId"]);

   //Write the XML to a file so we can look at it later

   ds.WriteXml("..\\..\\..\\SuppProd.xml",XmlWriteMode.WriteSchema);

   //load data into grid

   dataGrid1.DataSource=ds;

   dataGrid1.DataMember="suppliers";

   //create the XmlDataDocument

   doc=new XmlDataDocument(ds);

   //Select the productname elements and load them in the grid

   XmlNodeList nodeLst=doc.SelectNodes("//ProductName");

   foreach(XmlNode nd in nodeLst)

      listBox1.Items.Add(nd.InnerXml);

}

在这个示例中,在XMLProducts数据集中创建了两个DataTableProductsSuppliers。关系是Suppliers提供Products。在两个表的SupplierId列上创建一个新的关系,此时DataSet如图23-10所示。

 23-10

执行与上一个示例相同的WriteXml方法调用,得到如下XML文件(SuppProd.xml)

<?xml version="1.0" standalone="yes"?>

<XMLProducts>

   <xs:schema id="XMLProducts" xmlns="" 

              xmlns:xs="http://www.w3.org/2001/XMLSchema"

              xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

      <xs:element FTEL="XMLProducts" msdata:IsDataSet="true">

         <xs:complexType>

            <xs:choice maxOccurs="unbounded">

               <xs:element FTEL="products">

                  <xs:complexType>

                     <xs:sequence>

                        <xs:element FTEL="ProductID" type="xs:int"

                                    minOccurs="0" />

                        <xs:element FTEL="ProductName" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="SupplierID" type="xs:int"

                                    minOccurs="0" />

                        <xs:element FTEL="CategoryID" type="xs:int"

                                    minOccurs="0" />

                        <xs:element FTEL="QuantityPerUnit" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="UnitPrice" type="xs:decimal"

                                    minOccurs="0" />

                        <xs:element FTEL="UnitsInStock" type="xs:short"

                                    minOccurs="0" />

                        <xs:element FTEL="UnitsOnOrder" type="xs:short"

                                    minOccurs="0" />

                        <xs:element FTEL="ReorderLevel" type="xs:short"

                                    minOccurs="0" />

                        <xs:element FTEL="Discontinued" type="xs:boolean"

                                    minOccurs="0" />

                     </xs:sequence>

                  </xs:complexType>

               </xs:element>

               <xs:element FTEL="suppliers">

                  <xs:complexType>

                     <xs:sequence>

                        <xs:element FTEL="SupplierID" type="xs:int"

                                    minOccurs="0" />

                        <xs:element FTEL="CompanyName" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="ContactName" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="ContactTitle" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="Address" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="City" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="Region" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="PostalCode" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="Country" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="Phone" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="Fax" type="xs:string"

                                    minOccurs="0" />

                        <xs:element FTEL="HomePage" type="xs:string"

                                    minOccurs="0" />

                     </xs:sequence>

                  </xs:complexType>

               </xs:element>

            </xs:choice>

         </xs:complexType>

         <xs:unique FTEL="Constraint1">

            <xs:selector xpath=".//suppliers" />

            <xs:field xpath="SupplierID" />

         </xs:unique>

         <xs:keyref FTEL="Relation1" refer="Constraint1">

            <xs:selector xpath=".//products" />

            <xs:field xpath="SupplierID" />

         </xs:keyref>

      </xs:element>

   </xs:schema>

   <products>

      <ProductID>1</ProductID>

      <ProductName>Chai</ProductName>

      <SupplierID>1</SupplierID>

      <CategoryID>1</CategoryID>

      <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>

      <UnitPrice>18</UnitPrice>

      <UnitsInStock>39</UnitsInStock>

      <UnitsOnOrder>0</UnitsOnOrder>

      <ReorderLevel>10</ReorderLevel>

      <Discontinued>false</Discontinued>

   </products>

<suppliers>

      <SupplierID>1</SupplierID>

      <CompanyName>Exotic Liquids</CompanyName>

      <ContactName>Charlotte Cooper</ContactName>

      <ContactTitle>Purchasing Manager</ContactTitle>

      <Address>49 Gilbert St.</Address>

      <City>London</City>

      <PostalCode>EC1 4SD</PostalCode>

      <Country>UK</Country>

      <Phone>(171) 555-2222</Phone>

   </suppliers>

</XMLProducts>

该模式包含DataSet中的两个DataTable。数据包含两个表中的所有数据。为简洁起见,这里只显示了第一个supplierproducts记录。与以前一样,使用正确的XmlWriteMode参数可以只保存模式或数据。

www.zdexe.com

赞助商链接

热门内容

相关内容

联系我们

联系方式