Export (0) Print
Expand All

XPathExpression Class

Provides a typed class that represents a compiled XPath expression.

System.Object
  System.Xml.XPath.XPathExpression

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

public abstract class XPathExpression

The XPathExpression type exposes the following members.

  NameDescription
Public propertyExpressionWhen overridden in a derived class, gets a string representation of the XPathExpression.
Public propertyReturnTypeWhen overridden in a derived class, gets the result type of the XPath expression.
Top

  NameDescription
Public methodAddSort(Object, IComparer)When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.
Public methodAddSort(Object, XmlSortOrder, XmlCaseOrder, String, XmlDataType)When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters.
Public methodCloneWhen overridden in a derived class, returns a clone of this XPathExpression.
Public methodStatic memberCompile(String)Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression.
Public methodStatic memberCompile(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.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetContext(IXmlNamespaceResolver)When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution.
Public methodSetContext(XmlNamespaceManager)When overridden in a derived class, specifies the XmlNamespaceManager object to use for namespace resolution.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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

public class XPathExpressionExample
{
    public static void Main()
    {
        XPathDocument document = new XPathDocument("contosoBooks.xml");
        XPathNavigator navigator = document.CreateNavigator();

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

        XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
        manager.AddNamespace("bk", "http://www.contoso.com/books");

        expression1.SetContext(manager);
        expression2.SetContext(manager);

        Evaluate(expression1, navigator);
        Evaluate(expression2, navigator);
    }

    public static void Evaluate(XPathExpression expression, XPathNavigator navigator)
    {
        switch (expression.ReturnType)
        {
            case XPathResultType.Number:
                Console.WriteLine(navigator.Evaluate(expression));
                break;

            case XPathResultType.NodeSet:
                XPathNodeIterator nodes = navigator.Select(expression);
                while (nodes.MoveNext())
                {
                    Console.WriteLine(nodes.Current.ToString());
                }
                break;

            case XPathResultType.Boolean:
                if ((bool)navigator.Evaluate(expression))
                    Console.WriteLine("True!");
                break;

            case XPathResultType.String:
                Console.WriteLine(navigator.Evaluate(expression));
                break;
        }
    }
}

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>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft