Microsoft KB Archive/318547

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

Article ID: 318547

Article Last Modified on 5/23/2005

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * 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 .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q318547



For a Microsoft Visual Basic .NET version of this article, see 317108.

IN THIS TASK
SUMMARY
 * Create the XML Document
 * Create the Visual C# .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 &quot;C:\books.xml&quot;.</li></ol>

back to the top

Create the Visual C# .NET Application
<ol> <li>Create a new Visual C# .NET Windows Application project.</li> <li>Drag a command button control from the toolbox to the designer surface of Form1.cs.</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. System.Xml.XPath.XPathDocument xmldoc = new System.Xml.XPath.XPathDocument(&quot;c:\\books.xml&quot;);

//Create the XPathNavigator. System.Xml.XPath.XPathNavigator nav = xmldoc.CreateNavigator;

System.Xml.XPath.XPathNodeIterator iterator;

//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 (System.Xml.XPath.XPathException XPathExp) {                       //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); return; }           //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. while (iterator.MoveNext) {                       System.Diagnostics.Debug.WriteLine(iterator.Current.Value); }                 } catch (System.Exception otherExp) {           //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); return; }                   </li> <li>Read the inline comments to understand the functionality of the code. Pay particular attention to the try...catch 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 usage of the higher-level generic try...catch 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 which 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 C# .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 is displayed in the Visual Studio .NET Output window: XPathException: 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 Q318547.Form1.button1_Click(Object sender, EventArgs e) in <path of VC#.NET form>:line </li></ol>

back to the top

<div class="references_section">