Article ID: 317108
Article Last Modified on 5/23/2005
APPLIES TO
- Microsoft .NET Framework Class Libraries 1.1
- Microsoft .NET Framework Class Libraries 1.0
- Microsoft Visual Studio .NET 2002 Professional Edition
- Microsoft Visual Studio .NET 2002 Enterprise Architect
- Microsoft Visual Studio .NET 2002 Enterprise Developer
- Microsoft Visual Studio .NET 2002 Academic Edition
- Microsoft Visual Studio .NET 2003 Enterprise Architect
- Microsoft Visual Studio .NET 2003 Enterprise Developer
- Microsoft Visual Studio .NET 2003 Academic Edition
This article was previously published under Q317108
For a Microsoft Visual C# .NET version of this article, see 318547.
IN THIS TASK
SUMMARY
This step-by-step article demonstrates how to write exception handling code to trap and handle an XPathException (System.Xml.XPath.XPathException). Writing error handling code to catch an XPathException when executing the Select and Compile methods of the XPathNavigator object will help to identify exceptions caused by an invalid XPath query expression.
back to the top
Create the XML Document
Use Notepad to create a new XML document that contains the following code:
<?xml version='1.0'?> <Books> <Book> <Title>Beginning XML</Title> <Publisher>Wrox</Publisher> </Book> <Book> <Title>XML Step by Step</Title> <Publisher>MSPress</Publisher> </Book> <Book> <Title>Professional XML</Title> <Publisher>Wrox</Publisher> </Book> <Book> <Title>Developing XML solutions</Title> <Publisher>MSPress</Publisher> </Book> </Books>
- Save the document as books.xml in the root folder of your hard disk.
Create the Visual Basic .NET Application
- Create a new Visual Basic .NET Windows Application project.
- Drag a command button control from the toolbox to the designer surface of Form1.vb.
Paste the following code in the Click event procedure of the command button:
Try 'Construct the XPathDocument by specifying the path to books.xml. Dim xmldoc As New System.Xml.XPath.XPathDocument("c:\books.xml") 'Create the XPathNavigator. Dim nav As System.Xml.XPath.XPathNavigator = xmldoc.CreateNavigator() Dim iterator As System.Xml.XPath.XPathNodeIterator 'Error handling code to catch an XPathException that could be generated when an 'invalid XPath query expression is specified in the call to the Select method 'of the XPathNavigator. Try 'Create the XPathNodeIterator by executing the Select method of the XPathNavigator. iterator = nav.Select("//Publisher[. = 'MSPress']/parent:node()/Title") Catch XPathExp As System.Xml.XPath.XPathException 'Catch the XPathException and write it to the Visual Studio .NET output window. System.Diagnostics.Debug.WriteLine("XPathException:") System.Diagnostics.Debug.WriteLine("***************") System.Diagnostics.Debug.WriteLine(XPathExp.ToString) Exit Sub End Try 'Proceed to process results if no XPathException is raised. System.Diagnostics.Debug.WriteLine("Titles published by MSPress...") System.Diagnostics.Debug.WriteLine("******************************") 'Use the iterator to navigate the generated resultset. Do While iterator.MoveNext System.Diagnostics.Debug.WriteLine(iterator.Current.Value) Loop Catch otherExp As System.Exception 'Error handling code to catch other exceptions. System.Diagnostics.Debug.WriteLine("Other Exception:") System.Diagnostics.Debug.WriteLine("****************") System.Diagnostics.Debug.WriteLine(otherExp.ToString) Exit Sub End Try
- Read the inline comments to understand the functionality of the code. Pay particular attention to the Try...Catch...End Try exception handling code that encapsulates the call to the Select method of the XPathNavigator object. Note that this code is written to specifically catch an XPathException that could be raised when an invalid XPath expression is supplied as the query expression parameter of the Select method. The XPath query expression specified in the above sample is invalid. The axis parent and the node test node() should be separated by a double colon (::) instead of a single colon (:)
- Also note the use of the higher-level generic Try...Catch...End Try exception handling code to trap and handle other exceptions that could be raised by statements other than the call to the Select method of the XPathNavigator object. For instance, modifying the path to the source XML file to specify the name of a nonexistent file would raise a System.IO.FileNotFoundException exception that would be handled by this code. This example is written to catch an instance of the generic System.Exception class. You could write additional Catch blocks to handle other specific .NET exceptions or your own custom exceptions as required by your application.
Test the Code
- Save the changes to the Visual Basic .NET project and then run it.
When the form is displayed, click the command button to execute the code. The following output generated by the error handling code in the Catch block to trap an XPathException will be displayed in the Visual Studio .NET Output window:
System.Xml.XPath.XPathException: '//Publisher[. = 'MSPress']/parent:node()/Title' has an invalid token. at System.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion) at System.Xml.XPath.QueryBuilder.Build(String query, Boolean allowVar, Boolean allowKey) at System.Xml.XPath.QueryBuilder.Build(String query, Boolean& hasPrefix) at System.Xml.XPath.XPathNavigator.Compile(String xpath) at System.Xml.XPath.XPathNavigator.Select(String xpath) at XPathRoadMap.Form1.Button3_Click(Object sender, EventArgs e) in <Path to the VB.Net code module>:line <line number>
REFERENCES
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
313828 INFO: Roadmap for Executing XPath Queries in .NET Applications
Keywords: kbhowtomaster KB317108