XML Processing With The XMLReader Object, Part 2 - Of Nodes and Trees
(Page 4 of 6 )
Now, you've already seen two of the three built-in objects based on the core XmlReader object -- the XmlTextReader and XmlValidatingReader objects. This leaves us with the third and final object in this family to deal with: the XmlNodeReader object.
The XmlNodeReader class allows you to read data from any node of a DOM tree present in memory. Now you may, as I did initially, question the rationale behind having a reader for such a specific requirement -- after all, you can easily use the method and properties of the XmlDocument object to parse the file. But it's important to remember that DOM parsing is a processor-intensive task; therefore, using an XmlReader object (which is faster and not as resource-hungry as the regular DOM parser) can produce better results in some cases.
Second, it is not wise to assume that your application will always receive XML data in the form of a flat file or an XML data stream. XML data might even come to you in the form of a fragment of DOM tree. In such a scenario, it makes sense to use the XmlNodeReader object to read the contents of the node because of its speed and efficient performance.
Enough talk! Take a look at an example that uses the XmlNodeReader object.
<%@ Page Language="C#"%>
<%@ import namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void Page_Load() {
// define some variables
string strXmlFile = http://localhost:2121/xmlpull/library.xml;
// create an instance of the XmlDocument object
XmlDocument objXmlDoc = new XmlDocument();
// load an XML file into the XmlDocument object
objXmlDoc.Load(strXmlFile);
// load the NodeList object with the nodes required
XmlNodeList objNodeList = objXmlDoc.GetElementsByTagName("book");
// loop through the node list
// for each node, create an XmlNodeReader object
// to read the XML data from the file
foreach(XmlNode myNode in objNodeList) {
XmlNodeReader objXmlRdr = new XmlNodeReader(myNode);
ReadXmlNode(objXmlRdr);
objXmlRdr.Close();
}
}
void ReadXmlNode(XmlNodeReader objXmlRdr) {
string strSpaces = "";
while(objXmlRdr.Read()) {
// only process the elements
if(objXmlRdr.NodeType==XmlNodeType.Element) {
// reset the variable for a new node
strSpaces = "";
for(int count = 1; count <= objXmlRdr.Depth; count++) {
strSpaces += "===";
}
output.Text += strSpaces + "=> " + objXmlRdr.Name + "<br/>";
}
}
}
</script>
</head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>
This example is pretty straightforward. First, I have loaded the XML file into an XMLDocument object with the Load() method. I have then drilled down to the node of interest with the convenient GetElementsByTagName() method, which returns an XmlNodeList object that I can iterate over using a foreach loop, as shown below.
Now, each item in the XmlNodeList collection is an XmlNode object that can easily be used to create an instance of the XmlNodeReader object, the object that I'm interested in here. Since there will be multiple nodes resulting from this process, it make sense to place the corresponding code in a separate function which can be invoked repeatedly; hence the ReadXmlNode() function in the example above. Take a close look at this function, and you'll see that there isn't much difference between the method and properties of the XmlNodeReader object and the XmlTextReader object (the main difference lies in the source of the XML data they are capable of accepting)
Next: Playing Catch >>
More XML Articles
More By Harish Kamath (c) Melonfire