XPathExpression.Compile Method (String, IXmlNamespaceResolver)


Compiles the specified XPath expression, with the IXmlNamespaceResolver object specified for namespace resolution, and returns an XPathExpression object that represents the XPath expression.

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

public static XPathExpression Compile(
	string xpath,
	IXmlNamespaceResolver nsResolver


Type: System.String

An XPath expression.

Type: System.Xml.IXmlNamespaceResolver

An object that implements the IXmlNamespaceResolver interface for namespace resolution.

Exception Condition

The XPath expression parameter is not a valid XPath expression.


The XPath expression is not valid.

An XPath expression is evaluated to yield one of the following result types.


If a user-defined function with an invalid number of arguments (or an unimplemented user-defined function) is specified in the XPath expression, an exception occurs only at run-time. User-defined functions are not checked at compile time and exceptions resulting from user-defined functions occur only if execution evaluates the expression.


It is not possible to compile a XPathExpression with a custom context in one step by passing an XsltContext, which implements IXmlNamespaceResolver, as an argument to this method. To use a XPathExpression with a custom context, you must call the SetContext method after compiling the expression.

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