Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo XPathExpression.SetContext (XmlNamespaceManager)

 

Data di pubblicazione: ottobre 2016

Quando è sottoposto a override in una classe derivata, specifica l'oggetto XmlNamespaceManager da usare per la risoluzione dello spazio dei nomi.

Spazio dei nomi:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public abstract void SetContext(
	XmlNamespaceManager nsManager
)

Parametri

nsManager
Type: System.Xml.XmlNamespaceManager

Oggetto XmlNamespaceManager da usare per la risoluzione dello spazio dei nomi.

Exception Condition
XPathException

Il parametro dell'oggetto XmlNamespaceManager non è derivato dalla classe XmlNamespaceManager.

Namespace resolution is supported using the T:System.Xml.XmlNamespaceManager class which stores prefix and namespace Uniform Resource Identifier (URI) mappings. If the T:System.Xml.XPath.XPathExpression requires namespace resolution, the prefix and namespace URI pair must be added to the T:System.Xml.XmlNamespaceManager object and the M:System.Xml.XPath.XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method must be called to specify the T:System.Xml.XmlNamespaceManager object to use for namespace resolution.

The following are important notes to consider when using the M:System.Xml.XPath.XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method.

  • If the T:System.Xml.XPath.XPathExpression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still use the M:System.Xml.XPath.XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method and provide an T:System.Xml.XmlNamespaceManager object that contains a prefix and namespace URI to handle the default namespace.

  • You can also supply an T:System.Xml.IXmlNamespaceResolver object for namespace resolution to the M:System.Xml.XPath.XPathExpression.Compile(System.String,System.Xml.IXmlNamespaceResolver) method when you create your T:System.Xml.XPath.XPathExpression object.

  • SetContext accepts XsltContext as a namespace resolver, so you can implement a custom context and use functions and variables based on IXsltContextFunction and IXsltContextVariable. The XPath expression will execute them. For more information, see Funzioni e variabili definite dall'utente.

The following example shows how to use the XPath return type to determine how to process the XPath expression, and how to use the M:System.Xml.XPath.XPathExpression.SetContext(System.Xml.XmlNamespaceManager) method to provide an T:System.Xml.XmlNamespaceManager object for namespace resolution.

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

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>

.NET Framework
Disponibile da 1.1
Silverlight
Disponibile da 4.0
Torna all'inizio
Mostra: