Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
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 | 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 SqlConnection mySqlConnection new SqlConnection server localhost database Northwind uid sa pwd sa SqlCommand mySqlCommand mySqlConnection.CreateCommand mySqlCommand.CommandText SELECT TOP 2 CustomerlD CompanyName FROM Customers ORDER BY CustomerlD SELECT OrderlD CustomerlD ShipCountry FROM Orders WHERE CustomerlD IN SELECT TOP 2 CustomerlD FROM Customers ORDER BY CustomerlD SqlDataAdapter mySqlDataAdapter new SqlDataAdapter .