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", ""); test.XPathEvaluate("format-number(2, \"00\")", manager); 

How can I call the format-number function within an XPath statement?

3

1 Answer

Unfortunately, format-number function is not supported by MS XPath. It doesn't contain a lot of functions of XSLT. You can find all supporting functions in picture from post here.

But you can have to create your own function here. In article from above you can found my 3 function examples (if-else, format and current).

Sorry, my native language is Russian, but there is translation button. So, you need only take a look on the 1st image and check example code from the first download link. I hope you can copy code and implement your needed functionality for format-number.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.