XPathExpression Class


Provides a typed class that represents a compiled XPath expression.

Namespace:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)


Public MustInherit Class XPathExpression


When overridden in a derived class, gets a string representation of the XPathExpression.


When overridden in a derived class, gets the result type of the XPath expression.

System_CAPS_pubmethodAddSort(Object, IComparer)

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.

System_CAPS_pubmethodAddSort(Object, XmlSortOrder, XmlCaseOrder, String, XmlDataType)

When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters.


When overridden in a derived class, returns a clone of this XPathExpression.


Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression.

System_CAPS_pubmethodSystem_CAPS_staticCompile(String, IXmlNamespaceResolver)

Compiles the specified XPath expression, with the IXmlNamespaceResolver object specified for namespace resolution, and returns an XPathExpression object that represents the XPath expression.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution.


When overridden in a derived class, specifies the XmlNamespaceManager object to use for namespace resolution.


Returns a string that represents the current object.(Inherited from Object.)

This class is returned as the result of the Compile method of the XPathNavigator class. It is a parameter to the Evaluate, Matches, Select, and SelectSingleNode methods of the XPathNavigator class.

Notes to Inheritors:

When you inherit from the XPathExpression class, you must override the following members:

The following example shows how to use the XPath return type to determine how to process the XPath expression.

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

Public Class XPathExpressionExample

    Public Shared Sub Main()
        Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
        Dim navigator As XPathNavigator = document.CreateNavigator()

        Dim expression1 As XPathExpression = XPathExpression.Compile(".//bk:price/text()*10")  ' Returns a number.
        Dim expression2 As XPathExpression = XPathExpression.Compile("bk:bookstore/bk:book/bk:price")  ' Returns a nodeset.

        Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
        manager.AddNamespace("bk", "http://www.contoso.com/books")


        Evaluate(expression1, navigator)
        Evaluate(expression2, navigator)

    End Sub

    Public Shared Sub Evaluate(ByVal expression As XPathExpression, ByVal navigator As XPathNavigator)

        Select Case expression.ReturnType
            Case XPathResultType.Number
                Exit Sub

            Case XPathResultType.NodeSet
                Dim nodes As XPathNodeIterator = navigator.Select(expression)
                While nodes.MoveNext()
                End While

            Case XPathResultType.Boolean
                If CType(navigator.Evaluate(expression), Boolean) Then
                End If

            Case XPathResultType.String
        End Select

    End Sub
End Class

The example takes the contosoBooks.xml file as input.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>

.NET Framework
Available since 1.1
Available since 4.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top