XML Processing With The XMLReader Object, Part 2 - Playing Catch
(Page 5 of 6 )
To err is human -- which is why it's imperative that you include some mechanism in your ASP.NET code to handle errors that come up during script execution. And the next example does just that, using the ever-popular "try-catch" mechanism to trap any errors that might arise. Take a look:
<%@ Page Language="C#"%>
<%@ import namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void Page_Load() {
// create the XML Reader object
XmlTextReader objXmlRdr = null;
// start the "try" block
try {
// location of XML file
string strXmlFile = "http://localhost:2121/xmlpull/library.xml";
String strSpaces;
// create an instance of the XmlTextReader object
objXmlRdr = new XmlTextReader(strXmlFile);
objXmlRdr.WhitespaceHandling=WhitespaceHandling.None;
while(objXmlRdr.Read()) {
// only process the elements, ignore everything else
if(objXmlRdr.NodeType==XmlNodeType.Element) {
strSpaces = "";
for(int count = 1; count <= objXmlRdr.Depth; count++) {
strSpaces += "===";
}
output.Text += strSpaces + "=> " + objXmlRdr.Name + "<br/>";
}
}
} catch (XmlException e) {
output.Text = "An XML Exception occurred: " + e.Message;
} catch (Exception e) {
output.Text = "A General Exception occurred: " + e.Message;
} finally {
// close the XMLReader object
// if it exists
if(objXmlRdr != null) {
objXmlRdr.Close();
}
}
}
</script>
</head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>
If all goes well, the output shows the tree structure of the XML document instance. But now, introduce an deliberate error by deleting the library.xml file and look what happens:
A General Exception occured: The remote server returned an error: (404) Not Found.
Notice how the script take note of the absence of the XML file and displays a polite little message informing the user about the error.
Here's what you'd see if you didn't have an error-handling mechanism in place:

Not a pleasant sight at all!
Let's try another error -- "forget" to close the <library> element at the end of the file (thereby creating an XML document instance that is not well-formed) and look how the exception-handling mechanism reacts:
An XML Exception occurred: This is an unexpected token. The expected token is 'EndElement'. Line 15, position 3.
Most of the magic here lies in the "try-catch-finally" block, which does all the dirty work.
<%
// snip
// start the "try" block
try {
// process the XML file
// snip
} catch (XmlException e) {
output.Text = "An XML Exception occurred: " + e.Message;
} catch (Exception e) {
output.Text = "A General Exception occurred: " + e.Message;
} finally {
// close the XMLReader object
// if it exists
if(objXmlRdr != null) {
objXmlRdr.Close();
}
}
// snip
%>
First, you place all the code that processes the XML file -- creating the XmlTextReader object, loading and reading the XML file and so on -- in the "try" block.
This is followed by two catch blocks, one to handle an XmlException (these occur if something is wrong with the XML file itself) and another to handle any general Exception (such as a missing file).
Finally (pun intended), the finally block, which contains code that will always execute at the end of the "try" block (even if an exception takes place). This is the place for the code that closes objects and frees up vital system resources.
Next: Linking Out >>
More XML Articles
More By Harish Kamath (c) Melonfire