Export (0) Print
Expand All

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
[C#]
public virtual XPathExpression Compile(
 string xpath
);
[C++]
public: virtual XPathExpression* Compile(
 String* xpath
);
[JScript]
public function Compile(
   xpath : String
) : XPathExpression;

Parameters

xpath
A string representing an XPath expression.

Return Value

An XPathExpression object representing the XPath expression.

Exceptions

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

Remarks

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.

Example

[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")
      expr.SetContext(nsmgr)
      
      'Display the selection.
      Dim iterator As XPathNodeIterator = nav.Select(expr)
      While iterator.MoveNext()
         Console.WriteLine(iterator.Current.ToString())
      End While
   End Sub 'Main
End Class 'Sample 

[C#] 
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");
      expr.SetContext(nsmgr);

      //Display the selection.
      XPathNodeIterator iterator = nav.Select(expr);
      while (iterator.MoveNext()){
        Console.WriteLine(iterator.Current.ToString());
      }
   }

}

[C++] 
#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");
   expr->SetContext(nsmgr);

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

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>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

[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.

Requirements

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

Show:
© 2015 Microsoft