Nested XML

Chia sẻ: Tuan Nghia | Ngày: | Loại File: PDF | Số trang:6

0
57
lượt xem
5
download

Nested XML

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Nested XML

  1. 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 /* 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() {
  2. 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
  3. // (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"); } } 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 ALFKI Alfreds Futterkiste ANATR Ana Trujillo Emparedados y helados 10308 ANATR Mexico 10625 ANATR Mexico 10643 ALFKI Germany
  4. 10692 ALFKI Germany 10702 ALFKI Germany 10759 ANATR Mexico 10835 ALFKI Germany 10926 ANATR Mexico 10952 ALFKI Germany 11011 ALFKI Germany 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
  5. ALFKI Alfreds Futterkiste 10643 ALFKI Germany 10692 ALFKI Germany 10702 ALFKI Germany 10835 ALFKI Germany 10952 ALFKI Germany 11011 ALFKI Germany ANATR Ana Trujillo Emparedados y helados 10308 ANATR Mexico
  6. 10625 ANATR Mexico 10759 ANATR Mexico 10926 ANATR Mexico
Đồng bộ tài khoản