XPathExpression XPathExpression XPathExpression XPathExpression Class

定義

コンパイル済みの XPath 式を表す、型指定されたクラスを提供します。Provides a typed class that represents a compiled XPath expression.

public ref class XPathExpression abstract
public abstract class XPathExpression
type XPathExpression = class
Public MustInherit Class XPathExpression
継承
XPathExpressionXPathExpressionXPathExpressionXPathExpression

次の例は、xpath の戻り値の型を使用して、XPath 式の処理方法を決定する方法を示しています。The following example shows how to use the XPath return type to determine how to process the XPath expression.

public ref 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 ( *safe_cast<bool^>(nav->Evaluate( expr )) )
                        Console::WriteLine( "True!" );
            break;

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

};

int main()
{
   XPathDocument^ doc = gcnew XPathDocument( "contosoBooks.xml" );
   XPathNavigator^ nav = doc->CreateNavigator();
   XPathExpression^ expr1 = nav->Compile( ".//price/text()*10" ); // Returns a number.

   XPathExpression^ expr2 = nav->Compile( "bookstore/book/price" ); // Returns a nodeset.

   Sample^ MySample = gcnew Sample;
   MySample->Evaluate( expr1, nav );
   MySample->Evaluate( expr2, nav );
}

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;
        }
    }
}
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")

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

        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
                Console.WriteLine(navigator.Evaluate(expression))
                Exit Sub

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

            Case XPathResultType.Boolean
                If CType(navigator.Evaluate(expression), Boolean) Then
                    Console.WriteLine("True!")
                End If

            Case XPathResultType.String
                Console.WriteLine(navigator.Evaluate(expression))
        End Select

    End Sub
End Class

この例は、contosoBooks.xml ファイルを入力として使用します。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>  
  

注釈

このクラスは、 Compile XPathNavigatorクラスのメソッドの結果として返されます。This class is returned as the result of the Compile method of the XPathNavigator class. Evaluateこれは、クラスXPathNavigatorの、 MatchesSelectSingleNode、およびメソッドのパラメーターです。 SelectIt is a parameter to the Evaluate, Matches, Select, and SelectSingleNode methods of the XPathNavigator class.

注意 (継承者)

XPathExpressionクラスから継承する場合は、次のメンバーをオーバーライドする必要があります。When you inherit from the XPathExpression class, you must override the following members:

プロパティ

Expression Expression Expression Expression

派生クラスでオーバーライドされた場合、XPathExpressionstring 表現を取得します。When overridden in a derived class, gets a string representation of the XPathExpression.

ReturnType ReturnType ReturnType ReturnType

派生クラスでオーバーライドされた場合、XPath 式の結果型を取得します。When overridden in a derived class, gets the result type of the XPath expression.

メソッド

AddSort(Object, IComparer) AddSort(Object, IComparer) AddSort(Object, IComparer) AddSort(Object, IComparer)

派生クラスでオーバーライドされた場合、XPath 式で選択されたノードを、指定された IComparer オブジェクトに従って並べ替えます。When overridden in a derived class, sorts the nodes selected by the XPath expression according to the specified IComparer object.

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

派生クラスでオーバーライドされた場合、XPath 式で選択されたノードを、提供されたパラメーターに従って並べ替えます。When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters.

Clone() Clone() Clone() Clone()

派生クラスでオーバーライドされた場合、この XPathExpression の複製を返します。When overridden in a derived class, returns a clone of this XPathExpression.

Compile(String) Compile(String) Compile(String) Compile(String)

指定された XPath 式をコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression.

Compile(String, IXmlNamespaceResolver) Compile(String, IXmlNamespaceResolver) Compile(String, IXmlNamespaceResolver) Compile(String, IXmlNamespaceResolver)

指定された XPath 式を、名前空間の解決用に指定された IXmlNamespaceResolver オブジェクトを使用してコンパイルし、XPath 式を表す XPathExpression オブジェクトを返します。Compiles the specified XPath expression, with the IXmlNamespaceResolver object specified for namespace resolution, and returns an XPathExpression object that represents the XPath expression.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
SetContext(IXmlNamespaceResolver) SetContext(IXmlNamespaceResolver) SetContext(IXmlNamespaceResolver) SetContext(IXmlNamespaceResolver)

派生クラスでオーバーライドされた場合、名前の解決に使用する IXmlNamespaceResolver オブジェクトを指定します。When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution.

SetContext(XmlNamespaceManager) SetContext(XmlNamespaceManager) SetContext(XmlNamespaceManager) SetContext(XmlNamespaceManager)

派生クラスでオーバーライドされた場合、名前の解決に使用する XmlNamespaceManager オブジェクトを指定します。When overridden in a derived class, specifies the XmlNamespaceManager object to use for namespace resolution.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象