XML Processing with the XMLReader Object, Part 1 - Visiting the Library
(Page 3 of 5 )
I'll begin with a simple example - using an XmlTextReader to parse a static XML file. Here's the XML file, a list of books present in our technical library:
<?xml version='1.0'
<library>
<book id="MFRE001">
<title>XML and PHP</title>
<author>Vikram Vaswani</author>
<description>
Learn to manage your XML data with PHP
</description>
<price currency="USD">24.95</price>
</book>
<book id="MFRE002">
<title>
MySQL - The Complete Reference
</title>
<author>Vikram Vaswani</author>
<description>
Learn everything about this open source
database</description>
<price currency="USD">45.95</price>
</book>
</library>
And now for the ASP.NET code that will allow us to parse this XML file using the XmlTextReader object:
<%@ Page Language="C#"%>
<%@ import namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void Page_Load()
{
// location of XML file
string strXmlFile =
"http://localhost/xmlpull/library.xml";
// create an instance of the
// XmlTextReader object
XmlTextReader objXmlRdr = new XmlTextReader(strXmlFile);
// ignore whitespace in the XML file
objXmlRdr.WhitespaceHandling=WhitespaceHandling.None;
String strSpaces;
while(objXmlRdr.Read()) {
// only process the elements,
// ignore everything else
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/>";
}
}
// close the object and free up memory
objXmlRdr.Close();
}
</script>
</head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>
Before I get into the nitty-gritty of the code, here's what you should see when you run this script:

1. The first step is to import all the classes required to execute the application - the .NET libraries for the XML parser, which are part of the System.XML namespace.
<%@ import namespace="System.Xml"%>
2. Next, within the Page_Load() function, I have defined some variables and objects. The first is a string variable to store the location of the XML file, and the second is a local instance of the XmlTextReader object.
3. Finally, in order to tell the parser to ignore the whitespace present in the XML file, I set the "WhitespaceHandling" property of the XmlTextReader object to "None", as shown below:
<%
// location of XML file
string strXmlFile = "http://localhost/xmlpull/library.xml";
// create an instance of the
// XmlTextReader object
XmlTextReader objXmlRdr =
new XmlTextReader(strXmlFile);
// ignore whitespace in the XML file
objXmlRdr.WhitespaceHandling =
WhitespaceHandling.None;
%>
4. The next step is to read the XML file - a simple matter, since the object provides a Read() method for just this purpose. This method returns true if it encounters a node in the XML file. Once it is finished with the file, it returns false. This makes it easy to process an entire file, simply by wrapping the method call in a "while" loop.
<%
while(objXmlRdr.Read()) {
// process the XML data
}
%>
5. Of course, it doesn't make sense to read the entire file and not do anything with it. That's why, within the "while" loop, I've added the code to process element nodes and format them for display.
<%
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/>";
}
}
%>
The "NodeType" property of the current node can be used to filter out the elements for further processing. Note that if I hadn't included this condition at the beginning of the loop, the output would also contain processing instructions like this:
<?xml version='1.0'
Don't take my word for it - change the code and see for yourself!
The rest of the code in the "while" loop ensures that the output is formatted properly for display in the browser. Pay special attention to my use of the very cool "Depth" property, which holds an integer value specifying the depth of the current node in the tree hierarchy. Simply put, the element <library> is at depth 0, the element <book> is at depth 1, and so on.
Next: Digging Deeper >>
More XML Articles
More By Harish Kamath (c) Melonfire