Nested XML

As mentioned in Chapter 10, "Using DataSet Objects to Store Data," a DataSet contains two methods that output the contents of the DataRow objects as XML:

  • GetXml() returns the XML representation of the data stored in the DataSet object as a string.

  • WriteXml() writes the data from the DataSet object out to an XML file.

A DataRelation contains a property named Nested that gets or sets a bool value that indicates whether the DataRelation objects are nested. This is useful when defining hierarchical relationships in XML.

Specifically, when you set Nested to true, the child rows are nested within the parent rows in any XML that you output using the GetXml() and WriteXml() methods. Similarly, you can read the nested rows when calling the ReadXml() method of a DataSet to read an XML file.

The following example sets a DataRelation object's Nested property to true:

 myDataSet.Relations["CustomersOrders"].Nested = true; 

This is shown in Listing 12.1. Notice that this program writes two XML files named nonNestedXmlFile.xml and nestedXmlFile.xml. The nonNestedXmlFile.xml contains the default non-nested rows, and nestedXmlFile.xml contains the nested rows after the DataRelation object's Nested property is set to true.

Listing 12.1: NESTEDXML.CS

start example
 /*   NestedXml.cs illustrates how setting the Nested property   of a DataRelation to true causes the the child rows to be nested within the   parent rows in the output XML */ using System; using System.Data; using System.Data.SqlClient; class NestedXml {   public static void Main()   {     SqlConnection mySqlConnection =       new SqlConnection(         "server=localhost;database=Northwind;uid=sa;pwd=sa"       );     SqlCommand mySqlCommand = mySqlConnection.CreateCommand();     mySqlCommand.CommandText =       "SELECT TOP 2 CustomerID, CompanyName " +       "FROM Customers " +       "ORDER BY CustomerID;"  +       "SELECT OrderID, CustomerID, ShipCountry " +       "FROM Orders " +       "WHERE CustomerID IN (" +       "  SELECT TOP 2 CustomerID " +       "  FROM Customers " +       "  ORDER BY CustomerID " +       ")";     SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();     mySqlDataAdapter.SelectCommand = mySqlCommand;     DataSet myDataSet = new DataSet();     mySqlConnection.Open();     int numberOfRows = mySqlDataAdapter.Fill(myDataSet);     Console.WriteLine("numberOfRows = " + numberOfRows);     mySqlConnection.Close();     DataTable customersDT = myDataSet.Tables["Table"];     DataTable ordersDT = myDataSet.Tables["Table1"];     // create a DataRelation object named customersOrdersDataRel     DataRelation customersOrdersDataRel =       new DataRelation(         "CustomersOrders",         customersDT.Columns["CustomerID"],         ordersDT.Columns["CustomerID"]       );     myDataSet.Relations.Add(       customersOrdersDataRel     );     // write the XML out to a file     Console.WriteLine("Writing XML out to file nonNestedXmlFile.xml");     myDataSet.WriteXml("nonNestedXmlFile.xml");     // set the DataRelation object's Nested property to true     // (causes child rows to be nested in the parent rows of the     // XML output)     myDataSet.Relations["CustomersOrders"].Nested = true;     // write the XML out again (this time the child rows are nested     // within the parent rows)     Console.WriteLine("Writing XML out to file nestedXmlFile.xml");     myDataSet.WriteXml("nestedXmlFile.xml");   } } 
end example

Listing 12.2 shows the nonNestedXmlFile.xml file produced by the program. Notice that the parent rows from the Customers table are listed first, followed by the child rows from the Orders table. The child rows are not nested within the parent rows.

Listing 12.2: NONNESTEDXMLFILE.XML

start example
 <?xml version="1.0" standalone="yes"?> <NewDataSet>   <Table>     <CustomerID>ALFKI</CustomerID>     <CompanyName>Alfreds Futterkiste</CompanyName>   </Table>   <Table>     <CustomerID>ANATR</CustomerID>     <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>   </Table>   <Table1>     <OrderID>10308</OrderID>     <CustomerID>ANATR</CustomerID>     <ShipCountry>Mexico</ShipCountry>   </Table1>   <Table1>     <OrderID>10625</OrderID>     <CustomerID>ANATR</CustomerID>     <ShipCountry>Mexico</ShipCountry>   </Table1>   <Table1>     <OrderID>10643</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1>   <Table1>     <OrderID>10692</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1>   <Table1>     <OrderID>10702</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1>   <Table1>     <OrderID>10759</OrderID>     <CustomerID>ANATR</CustomerID>     <ShipCountry>Mexico</ShipCountry>   </Table1>   <Table1>     <OrderID>10835</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1>   <Table1>     <OrderID>10926</OrderID>     <CustomerID>ANATR</CustomerID>     <ShipCountry>Mexico</ShipCountry>   </Table1>   <Table1>     <OrderID>10952</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1>   <Table1>     <OrderID>11011</OrderID>     <CustomerID>ALFKI</CustomerID>     <ShipCountry>Germany</ShipCountry>   </Table1> </NewDataSet> 
end example

Listing 12.3 shows the nestedXmlFile.xml file produced by the program. Notice that this time the child rows from the Orders table are nested within the parent rows from the Customers table.

Listing 12.3: NESTEDXMLFILEL.CS

start example
 <?xml version="1.0" standalone="yes"?> <NewDataSet>   <Table>     <CustomerID>ALFKI</CustomerID>     <CompanyName>Alfreds Futterkiste</CompanyName>     <Table1>       <OrderID>10643</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>     <Table1>       <OrderID>10692</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>     <Table1>       <OrderID>10702</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>     <Table1>       <OrderID>10835</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>     <Table1>       <OrderID>10952</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>     <Table1>       <OrderID>11011</OrderID>       <CustomerID>ALFKI</CustomerID>       <ShipCountry>Germany</ShipCountry>     </Table1>   </Table>   <Table>     <CustomerID>ANATR</CustomerID>     <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>     <Table1>       <OrderID>10308</OrderID>       <CustomerID>ANATR</CustomerID>       <ShipCountry>Mexico</ShipCountry>     </Table1>     <Table1>       <OrderID>10625</OrderID>       <CustomerID>ANATR</CustomerID>       <ShipCountry>Mexico</ShipCountry>     </Table1>     <Table1>       <OrderID>10759</OrderID>       <CustomerID>ANATR</CustomerID>       <ShipCountry>Mexico</ShipCountry>     </Table1>     <Table1>       <OrderID>10926</OrderID>       <CustomerID>ANATR</CustomerID>       <ShipCountry>Mexico</ShipCountry>     </Table1>   </Table> </NewDataSet> 
end example




Mastering C# Database Programming
Mastering the SAP Business Information Warehouse: Leveraging the Business Intelligence Capabilities of SAP NetWeaver
ISBN: 0764596373
EAN: 2147483647
Year: 2003
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net