I am trying to evaluate an XPath expression which contains the function format-number
within the C# method XPathEvaluate(XNode, String)
. A simplified reproduction is as follows:
// In this example, the XML is not actually used; written purely to demonstrate the issue
XElement test = new XElement("test", 12);
object output = test.XPathEvaluate("format-number(2, \"00\")");
When this runs, the following exception is thrown:
System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'
I have tried to add the XSLT namespace to an XmlNamespaceManager
which is then provided to the XPathEvaluate
function, as suggested by the exception message and in an answer to a similar question, but that also did not work:
var manager = new XmlNamespaceManager(new NameTable());
manager.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
test.XPathEvaluate("format-number(2, \"00\")", manager);
How can I call the format-number
function within an XPath statement?