Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XPathExpression Class

Provides a typed class that represents a compiled XPath expression.

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

public abstract class XPathExpression

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;
        }
    }
}
__gc public class Sample 
{
public:
   static void Evaluate(XPathExpression * expr, XPathNavigator * nav)
   {
      XPathNodeIterator * i = nav -> Select(expr);
      switch(expr -> ReturnType) 
      {
      case XPathResultType::Number:
         Console::WriteLine(nav -> Evaluate(expr));
         break;

      case XPathResultType::NodeSet:
         while (i -> MoveNext()) 
            Console::WriteLine(i -> Current);
         break;    

      case XPathResultType::Boolean:
         if ((bool)nav -> Evaluate(expr))
            Console::WriteLine(S"True!");
         break;

      case XPathResultType::String:
         Console::WriteLine(nav -> Evaluate(expr));
         break;
      }
   }
};

int main()
{
   XPathDocument* doc = new XPathDocument(S"books.xml");
   XPathNavigator * nav = doc -> CreateNavigator();

   XPathExpression * expr1 = nav -> Compile(S".//price/text()*10"); // Returns a number.
   XPathExpression * expr2 = nav -> Compile(S"bookstore/book/price"); // Returns a nodeset.

   Sample * MySample = new Sample();

   MySample -> Evaluate(expr1, nav);
   MySample -> Evaluate(expr2, nav);
}

The example takes the contosoBooks.xml file as input.

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

System.Object
  System.Xml.XPath.XPathExpression

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft