Tải bản đầy đủ (.pdf) (6 trang)

Tài liệu Using an XmlDataDocument Object to Store an XML Document pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (23.95 KB, 6 trang )


Using an XmlDataDocument Object to Store an XML Document
In the previous section
, you saw how you use an XmlDocument object to store an XML
document containing customer details retrieved from a DataSet. That's fine, but wouldn't
it be great if you could combine the power of an XmlDocument with a DataSet? Well,
you can! That's where the XmlDataDocument class comes in.
You use an object of the XmlDataDocument class to access rows as both XmlNode
objects and relational DataRow objects. You associate a DataSet with your
XmlDataDocument by passing your DataSet to the XmlDataDocument constructor.
An XmlDataDocument object provides synchronization between the DataSet and the
XML document. For example, if you add a new customer as an XmlNode object to your
XmlDataDocument, then that customer is also added as a DataRow to your associated
DataSet. Similarly, if you add a new customer as a DataRow to your DataSet, then that
customer is also added as an XmlNode object in the XML document of the
XmlDataDocument. Also, if you update or delete a customer, then that change is made in
both the DataSet and the XmlDataDocument. You'll see examples of synchronization
shortly.
The XmlDataDocument class is derived from the XmlDocument class; therefore the
XmlDataDocument class inherits all the public properties, methods, and events shown in
the previous section
for the XmlDocument class. The DataSet property (type DataSet) is
the property added to the XmlDataDocument class. It gets the DataSet object, which
stored the relational representation of the data. You associate a DataSet with your
XmlDataDocument by passing the DataSet to the XmlDataDocument constructor. Table
16.8 shows the additional XmlDataDocument methods.
Table 16.8: XmlDataDocument Methods
Method Return
Type
Description
GetElementFromRow() 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. This
program 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 node's Country to USA, first setting the myDataSet object's
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 of
J9COM");
DataRow myDataRow = customersDT.NewRow();
myDataRow["CustomerID"] = "J9COM";
myDataRow["CompanyName"] = "J9 Company";
myDataRow["Country"] = "UK";
customersDT.Rows.Add(myDataRow);

// step 6: retrieve the J9COM node using GetElementFromRow()
Console.WriteLine("\nRetrieving J9COM node using GetElementFromRow()");
XmlNode myXmlNode = myXDD.GetElementFromRow(myDataRow);
Console.WriteLine("CustomerID = "+ myXmlNode.ChildNodes[0].InnerText);
Console.WriteLine("CompanyName = "+ myXmlNode.ChildNodes[1].InnerText);
Console.WriteLine("Country = "+ myXmlNode.ChildNodes[2].InnerText);

// step 7: set J9COM node's Country to USA, first setting
// EnforceConstraints to false
Console.WriteLine("\nSetting J9COM node's Country to USA");
myDataSet.EnforceConstraints = false;
myXmlNode.ChildNodes[2].InnerText = "USA";

// step 8: retrieve the ANATR XmlNode using SelectSingleNode()
Console.WriteLine("\nRetrieving ANATR node using SelectSingleNode()");
myXmlNode =
myXDD.SelectSingleNode(
"/NewDataSet/Customers[CustomerID=\" ANATR\"]"
);

// step 9: retrieve the ANATR DataRow using GetRowFromElement()

Console.WriteLine("\nRetrieving ANATR DataRow using GetRowFromElement()");
myDataRow =
myXDD.GetRowFromElement((XmlElement) myXmlNode);
foreach (DataColumn myDataColumn in customersDT.Columns)
{
Console.WriteLine(myDataColumn + "= "+
myDataRow[myDataColumn]);
}

// step 10: remove the ANATR node using RemoveAll()
Console.WriteLine("\nRemoving ANATR node");
myXmlNode.RemoveAll();

// step 11: display the XML document in myXDD using Save()
Console.WriteLine("\nXML document in myXDD:");
myXDD.Save(Console.Out);

// step 12: display the DataRow objects in customersDT using
// DisplayDataRows()
DisplayDataRows(customersDT);
}
}

The output from this program is as follows:
Customer DataRow objects in customersDT:
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
Country = Germany
CustomerID = ANATR
CompanyName = Ana Trujillo Emparedados y helados

Country = Mexico

XML document in myXDD:
<?xml version="1.0" encoding="IBM437"?>
<NewDataSet>
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<Country>Germany</Country>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<Country>Mexico</Country>
</Customers>
</NewDataSet>

Adding new DataRow to customersDT with CustomerID of J9COM

Retrieving J9COM node using GetElementFromRow()
CustomerID = J9COM
CompanyName = J9 Company
Country = UK

Setting J9COM node's Country to USA

×