XPathExpression.Compile 메서드

정의

지정된 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.

오버로드

Compile(String)

지정된 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.

Compile(String, IXmlNamespaceResolver)

네임스페이스 확인을 위해 지정된 IXmlNamespaceResolver 개체를 사용하여 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.

Compile(String)

지정된 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.

public:
 static System::Xml::XPath::XPathExpression ^ Compile(System::String ^ xpath);
public static System.Xml.XPath.XPathExpression Compile (string xpath);
static member Compile : string -> System.Xml.XPath.XPathExpression
Public Shared Function Compile (xpath As String) As XPathExpression

매개 변수

xpath
String

XPath 식입니다.

반환

XPathExpression

XPathExpression 개체입니다.

예외

XPath 식 매개 변수가 올바른 XPath 식이 아닙니다.

XPath 식이 잘못되었습니다.

예제

다음 예제에서는 XPath 반환 형식을 사용하여 XPath 식을 처리하는 방법을 결정하는 방법을 보여줍니다. 이 예제에서는 메서드를 Compile 사용하여 새 XPathExpression 개체를 반환합니다.

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 파일을 입력으로 사용합니다.

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

설명

XPath 식은 다음 결과 형식 중 하나를 생성하도록 계산됩니다.

참고

잘못된 수의 인수(또는 구현되지 않은 사용자 정의 함수)가 있는 사용자 정의 함수가 XPath 식에 지정된 경우 런타임에만 예외가 발생합니다. 사용자 정의 함수는 컴파일 시간에 확인되지 않으며 사용자 정의 함수에서 발생하는 예외는 실행이 식을 평가하는 경우에만 발생합니다.

추가 정보

적용 대상

Compile(String, IXmlNamespaceResolver)

네임스페이스 확인을 위해 지정된 IXmlNamespaceResolver 개체를 사용하여 XPath 식을 컴파일하고 XPath 식을 나타내는 XPathExpression 개체를 반환합니다.

public:
 static System::Xml::XPath::XPathExpression ^ Compile(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ nsResolver);
public static System.Xml.XPath.XPathExpression Compile (string xpath, System.Xml.IXmlNamespaceResolver? nsResolver);
public static System.Xml.XPath.XPathExpression Compile (string xpath, System.Xml.IXmlNamespaceResolver nsResolver);
static member Compile : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathExpression
Public Shared Function Compile (xpath As String, nsResolver As IXmlNamespaceResolver) As XPathExpression

매개 변수

xpath
String

XPath 식입니다.

nsResolver
IXmlNamespaceResolver

네임스페이스 확인을 위한 IXmlNamespaceResolver 인터페이스를 구현하는 개체입니다.

반환

XPathExpression

XPathExpression 개체입니다.

예외

XPath 식 매개 변수가 올바른 XPath 식이 아닙니다.

XPath 식이 잘못되었습니다.

설명

XPath 식은 다음 결과 형식 중 하나를 생성하도록 계산됩니다.

참고

잘못된 수의 인수(또는 구현되지 않은 사용자 정의 함수)가 있는 사용자 정의 함수가 XPath 식에 지정된 경우 런타임에만 예외가 발생합니다. 사용자 정의 함수는 컴파일 시간에 확인되지 않으며 사용자 정의 함수로 인한 예외는 실행이 식을 평가하는 경우에만 발생합니다.

참고

구현IXmlNamespaceResolver하는 를 이 메서드에 인수로 전달XsltContext하여 한 단계에서 사용자 지정 컨텍스트를 사용하여 컴파일 XPathExpression 할 수 없습니다. 사용자 지정 컨텍스트에서 XPathExpression 사용하려면 식을 컴파일한 SetContext 후 메서드를 호출해야 합니다.

추가 정보

적용 대상