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
/*
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"
// 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()
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