Danh mục

Using an XmlDataDocument Object to Store an XML Document

Số trang: 6      Loại file: pdf      Dung lượng: 23.95 KB      Lượt xem: 2      Lượt tải: 0    
10.10.2023

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (6 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Đó là nơi mà các lớp XmlDataDocument có in Bạn sử dụng một đối tượng của lớp XmlDataDocument để hàng truy cập như là cả hai đối tượng XmlNode và các đối tượng DataRow quan hệ. Bạn kết hợp một DataSet với XmlDataDocument của bạn bằng cách đi qua DataSet của bạn vào constructor XmlDataDocument. Một đối tượng XmlDataDocument cung cấp đồng bộ hóa giữa DataSet và tài liệu XML. Ví dụ, nếu bạn thêm một khách hàng mới là một đối tượng XmlNode để XmlDataDocument của bạn...
Nội dung trích xuất từ tài liệu:
Using an XmlDataDocument Object to Store an XML DocumentUsing an XmlDataDocument Object to Store an XML DocumentIn the previous section, you saw how you use an XmlDocument object to store an XMLdocument containing customer details retrieved from a DataSet. Thats fine, but wouldntit be great if you could combine the power of an XmlDocument with a DataSet? Well,you can! Thats where the XmlDataDocument class comes in.You use an object of the XmlDataDocument class to access rows as both XmlNodeobjects and relational DataRow objects. You associate a DataSet with yourXmlDataDocument by passing your DataSet to the XmlDataDocument constructor.An XmlDataDocument object provides synchronization between the DataSet and theXML document. For example, if you add a new customer as an XmlNode object to yourXmlDataDocument, then that customer is also added as a DataRow to your associatedDataSet. Similarly, if you add a new customer as a DataRow to your DataSet, then thatcustomer is also added as an XmlNode object in the XML document of theXmlDataDocument. Also, if you update or delete a customer, then that change is made inboth the DataSet and the XmlDataDocument. Youll see examples of synchronizationshortly.The XmlDataDocument class is derived from the XmlDocument class; therefore theXmlDataDocument class inherits all the public properties, methods, and events shown inthe previous section for the XmlDocument class. The DataSet property (type DataSet) isthe property added to the XmlDataDocument class. It gets the DataSet object, whichstored the relational representation of the data. You associate a DataSet with yourXmlDataDocument by passing the DataSet to the XmlDataDocument constructor. Table16.8 shows the additional XmlDataDocument methods. Table 16.8: XmlDataDocument MethodsMethod Return Description TypeGetElementFromRow() XmlElement Returns the XmlElement object associated with the specified DataRow object.GetRowFromElement() DataRow Returns the DataRow object associated with the specified XmlElement object.Load() void Overloaded. Loads information from the specified data source into the XmlDataDocument object and synchronizes the loaded data with the DataSet.Listing 16.18 shows a program that illustrates the use of an XmlDataDocument. Thisprogram performs the following steps: 1. Creates a DataSet object named myDataSet and fills it with a DataTable named customersDT that contains the top two rows from the Customers table. 2. Display the DataRow objects in customersDT using the DisplayDataRows() method, which is defined near the start of the program. 3. Creates an XmlDataDocument object named myXDD, passing myDataSet to the constructor; this associates myDataSet with the XmlDataDocument. 4. Displays the XML document in myXDD by passing Console.Out to the Save() method. 5. Adds a customer DataRow with a CustomerID of J9COM to customersDT. 6. Retrieves the J9COM node using the GetElementFromRow() method. This method accepts a DataRow as a parameter and returns the associated XmlNode. 7. Sets the J9COM nodes Country to USA, first setting the myDataSet objects EnforceConstraints property to false-which you must do before making any changes to nodes. 8. Retrieves the ANATR XmlNode using SelectSingleNode(). 9. Retrieves the ANATR DataRow using GetRowFromElement(). This method accepts an XmlElement as a parameter and returns the associated DataRow. 10. Removes the ANATR node using RemoveAll(). 11. Display the XML document in myXDD using Save(). 12. Display the DataRow objects in customersDT using DisplayDataRows().Listing 16.18: USINGXMLDATADOCUMENT.CS/* UsingXmlDataDocument.cs illustrates how to use an XmlDataDocument object*/using System;using System.Data;using System.Data.SqlClient;using System.Xml;class UsingXmlDataDocument{ public static void DisplayDataRows(DataTable myDataTable) { Console.WriteLine(\n\nCustomer DataRow objects in customersDT:); foreach (DataRow myDataRow in myDataTable.Rows) { foreach (DataColumn myDataColumn in myDataTable.Columns) { Console.WriteLine(myDataColumn + = + myDataRow[myDataColumn]); } }}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, Country + FROM Customers + ORDER BY CustomerID; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); mySqlDataAdapter.SelectCommand = mySqlCommand; // step 1: create a DataSet object and fill it with the top 2 rows // from the Customers table DataSet myDataSet = new DataSet(); mySqlConnection.Open(); mySqlDataAdapter.Fill(myDataSet, Customers); mySqlConnection.Close(); DataTable customersDT = myDataSet.Tables[Customers]; // step 2: display the DataRow objects in customersDT using // DisplayDataRows() DisplayDataRows(customersDT); // step 3: create an XmlDataDocument object, passing myDataSet // to the constructor; this associates myDataSet with the // XmlDataDocument XmlDataDocument myXDD = new XmlDataDocument(myDataSet); // step 4: display the XML document in myXDD Console.WriteLine(\nXML document in myXDD:); myXDD.Save(Console.Out); // step 5: add a customer DataRow to customersDT with a CustomerID // of J9COM Console.WriteLine(\n\nAdding new DataRow to customersDT with CustomerID ofJ9COM); DataRow myDataRow = customersDT.NewRow(); myDataRow[CustomerID] = J9CO ...

Tài liệu được xem nhiều: