XPathExpression 클래스

정의

컴파일된 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
상속
XPathExpression

예제

다음 예제에서는 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, Matches, Select, 및 SelectSingleNode 메서드를 XPathNavigator 클래스입니다.It 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

파생 클래스에서 재정의된 경우 XPathExpressionstring 표현을 가져옵니다.When overridden in a derived class, gets a string representation of the XPathExpression.

ReturnType

파생 클래스에서 재정의된 경우 XPath 식의 결과 형식을 가져옵니다.When overridden in a derived class, gets the result type of the XPath expression.

메서드

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)

파생 클래스에서 재정의된 경우 XPath 식에서 선택한 노드를 전달된 매개 변수에 따라 정렬합니다.When overridden in a derived class, sorts the nodes selected by the XPath expression according to the supplied parameters.

Clone()

파생 클래스에서 재정의된 경우 이 XPathExpression의 복제를 반환합니다.When overridden in a derived class, returns a clone of this XPathExpression.

Compile(String)

지정된 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.Compiles the XPath expression specified and returns an XPathExpression object representing the XPath expression.

Compile(String, IXmlNamespaceResolver)

네임스페이스 확인을 위해 지정된 IXmlNamespaceResolver 개체를 사용하여 XPath 식을 컴파일하고 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)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
SetContext(IXmlNamespaceResolver)

파생 클래스에서 재정의된 경우 네임스페이스 확인에 사용할 IXmlNamespaceResolver 개체를 지정합니다.When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution.

SetContext(XmlNamespaceManager)

파생 클래스에서 재정의된 경우 네임스페이스 확인에 사용할 XmlNamespaceManager 개체를 지정합니다.When overridden in a derived class, specifies the XmlNamespaceManager object to use for namespace resolution.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.Returns a string that represents the current object.

(다음에서 상속됨 Object)

적용 대상