An XmlDocument object stores the nodes of the XML document in objects of the XmlNode class.. Table 16.5 shows some of the XmlDocument properties; Table 16.6 shows some of the XmlDocument
Trang 1Using an XmlDocument Object to Store an XML Document
You use an object of the XmlDocument class to represent an XML document in a C# program An XmlDocument object stores the nodes of the XML document in objects of the XmlNode class You can, for example, load rows from the database into a DataSet object, and then load an XML representation of those rows into an XmlDocument object Table 16.5 shows some of the XmlDocument properties; Table 16.6 shows some of the XmlDocument methods; and Table 16.7 shows the XmlDocument events
Table 16.5: XmlDocument Properties
that contains the attributes of the current node
XML document
declaration
has any child nodes
XML document
node and all of its children
children of the current node
current node is read-only
Trang 2Table 16.5: XmlDocument Properties
with the XML implementation
current node
node and all of its children
current node belongs to
current node
whether white space is to be preserved when XML is loaded or saved The default
is false
current node
resolving external resources
Table 16.6: XmlDocument Methods
of child nodes
the specified name
object with the specified data
with the specified data
CreateDocumentFragment() XmlDocumentFragment Creates an
XmlDocumentFragment object with the specified data
Trang 3Table 16.6: XmlDocument Methods
object with the specified data
XmlElement object
object with the specified name
that you can use to navigate the XML document
object
the specified text
the specified ID
XmlNodeList object that contains
a list of all descendant elements that match the specified name
declaration with the specified prefix that is in scope for the current node, and then returns the namespace URI
declaration with the specified namespace URI that is in scope for the current node, and then returns the prefix
document into the current XML document
immediately after the specified
Trang 4Table 16.6: XmlDocument Methods
reference node
immediately before the specified reference node
your XmlDocument object
the specified string into your XmlDocument object
beginning of the child nodes
on the information in a specified XmlReader object Your
XmlReader must be positioned on
a node or attribute
attributes of the current node
another
document to the specified location
matching the specified XPath expression
XmlNode that matches the specified XPath expression
document to the specified XmlWriter object
specified XmlWriter object
Table 16.7: XmlDocument Events
Trang 5Event Event Handler Description
NodeChanging XmlNodeChangedEventHandler Fires before a value in a node is
changed
NodeChanged XmlNodeChangedEventHandler Fires after a value in a node is
changed
NodeInserting XmlNodeChangedEventHandler Fires before a node is inserted
NodeInserted XmlNodeChangedEventHandler Fires after a node is inserted
NodeRemoving XmlNodeChangedEventHandler Fires before a node is removed
NodeRemoved XmlNodeChangedEventHandler Fires after a node is removed
Listing 16.17 shows a program that illustrates the use of an XmlDocument object This program performs the following steps:
1 Creates a DataSet object named myDataSet and fills it with the top two rows from the Customers table
2 Creates an XmlDocument object named myXmlDocument, and then loads it with the XML from myDataSet You can use the GetXml() method to return the
customer rows in myDataSet as a string containing a complete XML document You can then use the output string from GetXml() as the input to the LoadXml() method of myXmlDocument; this loads myXmlDocument with the XML
document containing the customer details
3 Displays the XML in myXmlDocument using the Save() method, passing
Console.Out to the Save() method This results in the XML document being displayed on the screen
4 Retrieves the XmlNode objects in myXmlDocument using the SelectNodes() method, and then displays the text contained in the child nodes of each XmlNode using the InnerText property You pass an XPath expression to SelectNodes() to retrieve the required nodes
5 Retrieves the XmlNode for the ANATR customer using the SelectSingleNode() method, and displays the text contained in the child nodes of this XmlNode You pass an XPath expression to SelectSingleNode() to retrieve the required node Listing 16.17: USINGXMLDOCUMENT.CS
/*
UsingXmlDocument.cs illustrates the use of an XmlDocument object
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
Trang 6class UsingXmlDocument
{
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();
// step 2: create an XmlDocument object and load it with the XML from // the DataSet; the GetXml() method returns the rows in
// myDataSet as a string containing a complete XML document; and
// the LoadXml() method loads myXmlDocument with the XML document // string returned by GetXml()
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.LoadXml(myDataSet.GetXml());
// step 3: display the XML in myXmlDocument using the Save() method Console.WriteLine("Contents of myXmlDocument:");
myXmlDocument.Save(Console.Out);
// step 4: retrieve the XmlNode objects in myXmlDocument using the // SelectNodes() method; you pass an XPath expression to SelectNodes() Console.WriteLine("\n\nCustomers:");
foreach (XmlNode myXmlNode in
myXmlDocument.SelectNodes("/NewDataSet/Customers"))
{
Console.WriteLine("CustomerID = "+
myXmlNode.ChildNodes[0].InnerText);
Trang 7Console.WriteLine("CompanyName = "+
myXmlNode.ChildNodes[1].InnerText);
Console.WriteLine("Country = "+
myXmlNode.ChildNodes[2].InnerText);
}
// step 5: retrieve the XmlNode for the ANATR customer using
// the SelectSingleNode() method; you pass an XPath
// expression to SelectSingleNode
Console.WriteLine("\nRetrieving node with CustomerID of ANATR");
XmlNode myXmlNode2 =
myXmlDocument.SelectSingleNode(
"/NewDataSet/Customers[CustomerID=\" ANATR\"]"
);
Console.WriteLine("CustomerID = "+
myXmlNode2.ChildNodes[0].InnerText);
Console.WriteLine("CompanyName = "+
myXmlNode2.ChildNodes[1].InnerText);
Console.WriteLine("Country = "+
myXmlNode2.ChildNodes[2].InnerText);
}
}
Remember, you'll need to change the connection string for your SqlConnection object to connect to your database near the start of this program
The output from this program is as follows:
Contents of myXmlDocument:
<?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>
Trang 8Customers:
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
Country = Germany
CustomerID = ANATR
CompanyName = Ana Trujillo Emparedados y helados Country = Mexico
Retrieving node with CustomerID of ANATR
CustomerID = ANATR
CompanyName = Ana Trujillo Emparedados y helados Country = Mexico