XPathExpression.SetContext Method (IXmlNamespaceResolver)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

When overridden in a derived class, specifies the IXmlNamespaceResolver object to use for namespace resolution.

Namespace:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public abstract void SetContext(
	IXmlNamespaceResolver nsResolver


Type: System.Xml.IXmlNamespaceResolver

An object that implements the IXmlNamespaceResolver interface to use for namespace resolution.

Exception Condition

The IXmlNamespaceResolver object parameter is not derived from IXmlNamespaceResolver.

Namespace resolution is supported using classes that implement the IXmlNamespaceResolver interface, such as the XmlNamespaceManager class. The XmlNamespaceManager stores prefix and namespace Uniform Resource Identifier (URI) mappings. If the XPathExpression requires namespace resolution, the prefix and namespace URI pair must be added to an object, such as the XmlNamespaceManager class, which implements the IXmlNamespaceResolver interface, and the SetContext method must be called to specify the IXmlNamespaceResolver interface object to use for namespace resolution.

The following are important notes to consider when using the SetContext method.

  • If the 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 SetContext method and provide an object that contains a prefix and namespace URI to handle the default namespace.

  • You can also supply an IXmlNamespaceResolver interface object for namespace resolution to the Compile method when you create your 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 User Defined Functions and Variables.

The following example shows how to use the XPath return type to determine how to process the XPath expression, and how to use the SetContext method to provide an 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");


        Evaluate(expression1, navigator);
        Evaluate(expression2, navigator);

    public static void Evaluate(XPathExpression expression, XPathNavigator navigator)
        switch (expression.ReturnType)
            case XPathResultType.Number:

            case XPathResultType.NodeSet:
                XPathNodeIterator nodes = navigator.Select(expression);
                while (nodes.MoveNext())

            case XPathResultType.Boolean:
                if ((bool)navigator.Evaluate(expression))

            case XPathResultType.String:

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>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>

.NET Framework
Available since 2.0
Available since 4.0
Return to top