Microsoft KB Archive/317108

= HOW TO: Use the XPathException Class in Visual Basic .NET =

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
 * Create the XML Document
 * Create the Visual Basic .NET Application
 * Test the Code

REFERENCES



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:    Beginning XML Wrox   XML Step by Step MSPress</Publisher> </Book>  Professional XML</Title> Wrox</Publisher> </Book>  Developing XML solutions</Title> MSPress</Publisher> </Book> </Books> </li> <li>Save the document as books.xml in the root folder of your hard disk.</li></ol>

back to the top

Create the Visual Basic .NET Application
<ol> <li>Create a new Visual Basic .NET Windows Application project.</li> <li>Drag a command button control from the toolbox to the designer surface of Form1.vb.</li> <li> 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(&quot;c:\books.xml&quot;)

'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(&quot;//Publisher[. = 'MSPress']/parent:node/Title&quot;)

Catch XPathExp As System.Xml.XPath.XPathException 'Catch the XPathException and write it to the Visual Studio .NET output window. System.Diagnostics.Debug.WriteLine(&quot;XPathException:&quot;) System.Diagnostics.Debug.WriteLine(&quot;***************&quot;) System.Diagnostics.Debug.WriteLine(XPathExp.ToString) Exit Sub End Try

'Proceed to process results if no XPathException is raised. System.Diagnostics.Debug.WriteLine(&quot;Titles published by MSPress...&quot;) System.Diagnostics.Debug.WriteLine(&quot;******************************&quot;)

'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(&quot;Other Exception:&quot;) System.Diagnostics.Debug.WriteLine(&quot;****************&quot;) System.Diagnostics.Debug.WriteLine(otherExp.ToString) Exit Sub End Try </li> <li>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 (:)</li> <li>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.</li></ol>

back to the top

Test the Code
<ol> <li>Save the changes to the Visual Basic .NET project and then run it.</li> <li> 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 </li></ol>

back to the top

<div class="references_section">