title: How to use XPath function in a XPathExpression instance programatically?
body: <p>My current program need to use programatically create a XPathExpression instance to apply to XmlDocument. The xpath needs to use some XPath functions like "ends-with". However, I cannot find a way to use "ends-with" in XPath. I</p>
  
  <p>It throw exception like below</p>
  
  <blockquote>
    <p>Unhandled Exception:
    System.Xml.XPath.XPathException:
    Namespace Manager or XsltC ontext
    needed. This query has a prefix,
    variable, or user-defined function.<br>
    at
    MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree()
    at
    System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression
    expr, XPathNodeIt erator context)<br>
    at
    System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression
    expr)</p>
  </blockquote>
  
  <p>The code is like this:</p>
  
  <pre><code>    XmlDocument xdoc = new XmlDocument();
      xdoc.LoadXml(@"&lt;?xml version=""1.0"" encoding=""utf-8"" ?&gt;
                          &lt;myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""&gt; 
                          &lt;data&gt;Hello World&lt;/data&gt;
                      &lt;/myXml&gt;");
      XPathNavigator navigator = xdoc.CreateNavigator();
  
      XPathExpression xpr;
      xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')");
  
      object result = navigator.Evaluate(xpr);
      Console.WriteLine(result);
  </code></pre>
  
  <p>I tried to change the code to insert XmlNamespaceManager when compiling the expression, like below</p>
  
  <pre><code>    XmlDocument xdoc = new XmlDocument();
      xdoc.LoadXml(@"&lt;?xml version=""1.0"" encoding=""utf-8"" ?&gt;
                          &lt;myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""&gt; 
                          &lt;data&gt;Hello World&lt;/data&gt;
                      &lt;/myXml&gt;");
      XPathNavigator navigator = xdoc.CreateNavigator();
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
      nsmgr.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");
  
      XPathExpression xpr;
      xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')", nsmgr);
  
      object result = navigator.Evaluate(xpr);
      Console.WriteLine(result);
  </code></pre>
  
  <p>It fails on XPathExpression.Compile invocation:</p>
  
  <blockquote>
    <p>Unhandled Exception:
    System.Xml.XPath.XPathException:
    XsltContext is needed for this query
    because of an unknown function.    at
    MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFuncti
    on(String prefix, String name,
    XPathResultType[] ArgTypes)    at
    MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext
    context)    at
    MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager
    nsM anager)    at
    System.Xml.XPath.XPathExpression.Compile(String
    xpath, IXmlNamespaceResolv er
    nsResolver)</p>
  </blockquote>
  
  <p>Anybody know the trick to use off-the-shelf XPath functions with XPathExpression.Compile?
  Thanks</p>