This documentation is archived and is not being maintained.

XPathNavigator.Compile Method

Compiles a string representing an XPath expression and returns an XPathExpression.

[Visual Basic]
Public Overridable Function Compile( _
   ByVal xpath As String _
) As XPathExpression
public virtual XPathExpression Compile(
 string xpath
public: virtual XPathExpression* Compile(
 String* xpath
public function Compile(
   xpath : String
) : XPathExpression;


A string representing an XPath expression.

Return Value

An XPathExpression object representing the XPath expression.


Exception Type Condition
ArgumentException The xpath parameter contains an invalid XPath expression.


An XPath expression is evaluated to yield one of the following return types:

  • Node Set - an unordered collection of nodes without duplicates
  • Boolean - true or false
  • Number - a floating-point number
  • String - a sequence of UCS characters

Expressions that return a node set can be used in the Select and Evaluate methods. Expressions that return a boolean, number, or string can be used in the Evaluate method. The rules on valid expressions for the Matches method are specific to that method.


[Visual Basic, C#, C++] The following example displays all ISBN attribute nodes.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath

Public Class Sample
   Public Shared Sub Main()
      Dim doc As New XPathDocument("booksort.xml")
      Dim nav As XPathNavigator = doc.CreateNavigator()
      'Select all ISBN attributes.
      Dim expr As XPathExpression
      expr = nav.Compile("/bookstore/book/@bk:ISBN")
      Dim nsmgr As New XmlNamespaceManager(nav.NameTable)
      nsmgr.AddNamespace("bk", "urn:samples")
      'Display the selection.
      Dim iterator As XPathNodeIterator = nav.Select(expr)
      While iterator.MoveNext()
      End While
   End Sub 'Main
End Class 'Sample 

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Sample
  public static void Main()

      XPathDocument doc = new XPathDocument("booksort.xml");
      XPathNavigator nav = doc.CreateNavigator();

      //Select all ISBN attributes.
      XPathExpression expr; 
      expr = nav.Compile("/bookstore/book/@bk:ISBN");
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
      nsmgr.AddNamespace("bk", "urn:samples");

      //Display the selection.
      XPathNodeIterator iterator = nav.Select(expr);
      while (iterator.MoveNext()){


#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::XPath;

int main()

   XPathDocument* doc = new XPathDocument(S"booksort.xml");
   XPathNavigator* nav = doc->CreateNavigator();

   //Select all ISBN attributes.
   XPathExpression* expr; 
   expr = nav->Compile(S"/bookstore/book/@bk:ISBN");

   XmlNamespaceManager* nsmgr = new XmlNamespaceManager(nav->NameTable);
   nsmgr->AddNamespace(S"bk", S"urn:samples");

   //Display the selection.
   XPathNodeIterator* iterator = nav->Select(expr);
   while (iterator->MoveNext()){

The example uses the file, booksort.xml, as input.

<?xml version="1.0"?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XPathNavigator Class | XPathNavigator Members | System.Xml.XPath Namespace