XPathExpression.SetContext 方法

定义

在派生类中被重写时,指定要用于命名空间解析的 IXmlNamespaceResolver 对象。

重载

SetContext(IXmlNamespaceResolver)

在派生类中被重写时,指定要用于命名空间解析的 IXmlNamespaceResolver 对象。

SetContext(XmlNamespaceManager)

在派生类中被重写时,指定要用于命名空间解析的 XmlNamespaceManager 对象。

SetContext(IXmlNamespaceResolver)

Source:
XPathExpr.cs
Source:
XPathExpr.cs
Source:
XPathExpr.cs

在派生类中被重写时,指定要用于命名空间解析的 IXmlNamespaceResolver 对象。

public:
 abstract void SetContext(System::Xml::IXmlNamespaceResolver ^ nsResolver);
public abstract void SetContext (System.Xml.IXmlNamespaceResolver? nsResolver);
public abstract void SetContext (System.Xml.IXmlNamespaceResolver nsResolver);
abstract member SetContext : System.Xml.IXmlNamespaceResolver -> unit
Public MustOverride Sub SetContext (nsResolver As IXmlNamespaceResolver)

参数

nsResolver
IXmlNamespaceResolver

实现 IXmlNamespaceResolver 接口以用于命名空间解析的对象。

例外

示例

以下示例演示如何使用 XPath 返回类型来确定如何处理 XPath 表达式,以及如何使用 SetContext 方法为命名空间解析提供 XmlNamespaceManager 对象。

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>  

注解

使用实现 IXmlNamespaceResolver 接口的类(如 XmlNamespaceManager 类)支持命名空间解析。 存储 XmlNamespaceManager 前缀和命名空间统一资源标识符 (URI) 映射。 XPathExpression如果 需要命名空间解析,则必须将前缀和命名空间 URI 对添加到对象(如 XmlNamespaceManager 实现接口的 IXmlNamespaceResolver 类)中,并且SetContext必须调用 方法来指定要用于命名空间解析的IXmlNamespaceResolver接口对象。

下面是使用 SetContext 方法时要考虑的重要注意事项。

另请参阅

适用于

SetContext(XmlNamespaceManager)

Source:
XPathExpr.cs
Source:
XPathExpr.cs
Source:
XPathExpr.cs

在派生类中被重写时,指定要用于命名空间解析的 XmlNamespaceManager 对象。

public:
 abstract void SetContext(System::Xml::XmlNamespaceManager ^ nsManager);
public abstract void SetContext (System.Xml.XmlNamespaceManager nsManager);
abstract member SetContext : System.Xml.XmlNamespaceManager -> unit
Public MustOverride Sub SetContext (nsManager As XmlNamespaceManager)

参数

nsManager
XmlNamespaceManager

要用于命名空间解析的 XmlNamespaceManager 对象。

例外

XmlNamespaceManager 对象参数不是从 XmlNamespaceManager 类派生的。

示例

以下示例演示如何使用 XPath 返回类型来确定如何处理 XPath 表达式,以及如何使用 SetContext 方法为命名空间解析提供 XmlNamespaceManager 对象。

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>  

注解

使用 XmlNamespaceManager 存储前缀和命名空间统一资源标识符 (URI) 映射的 类支持命名空间解析。 XPathExpression如果需要命名空间解析,则必须将前缀和命名空间 URI 对添加到 对象,XmlNamespaceManager并且SetContext必须调用 方法来指定要XmlNamespaceManager用于命名空间解析的对象。

下面是使用 SetContext 方法时要考虑的重要注意事项。

另请参阅

适用于