Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 99175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:20:14+00:00 2026-05-11T00:20:14+00:00

My current program need to use programatically create a XPathExpression instance to apply to

  • 0

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

It throw exception like below

Unhandled Exception: System.Xml.XPath.XPathException: Namespace Manager or XsltC ontext needed. This query has a prefix, variable, or user-defined function.
at MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr, XPathNodeIt erator context)
at System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr)

The code is like this:

    XmlDocument xdoc = new XmlDocument();     xdoc.LoadXml(@'<?xml version=''1.0'' encoding=''utf-8'' ?>                         <myXml xmlns=''http://MyNamespace'' xmlns:fn=''http://www.w3.org/2005/xpath-functions''>                          <data>Hello World</data>                     </myXml>');     XPathNavigator navigator = xdoc.CreateNavigator();      XPathExpression xpr;     xpr = XPathExpression.Compile('fn:ends-with(/myXml/data, 'World')');      object result = navigator.Evaluate(xpr);     Console.WriteLine(result); 

I tried to change the code to insert XmlNamespaceManager when compiling the expression, like below

    XmlDocument xdoc = new XmlDocument();     xdoc.LoadXml(@'<?xml version=''1.0'' encoding=''utf-8'' ?>                         <myXml xmlns=''http://MyNamespace'' xmlns:fn=''http://www.w3.org/2005/xpath-functions''>                          <data>Hello World</data>                     </myXml>');     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); 

It fails on XPathExpression.Compile invocation:

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)

Anybody know the trick to use off-the-shelf XPath functions with XPathExpression.Compile? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:20:15+00:00Added an answer on May 11, 2026 at 12:20 am

    The function ends-with() is not defined for XPath 1.0 but only for XPath 2.0 and XQuery.

    You are using .NET. .NET at this date does not implement XPath 2.0, XSLT 2.0 or XQuery.

    One can easily construct an XPath 1.0 expression, the evaluation of which produces the same result as the function ends-with():

    $str2 = substring($str1, string-length($str1)- string-length($str2) +1)

    produces the same boolean result (true() or false()) as:

    ends-with($str1, $str2)

    In your concrete case you just need to substitute the right expressions for $str1 and $str2. They are, accordingly, /myXml/data and 'World'.

    So, the XPath 1.0 expression to use, that is equivalent to the XPath 2.0 expression ends-with(/myXml/data, 'World') is:

    'World' =     substring(/myXml/data,              string-length(/myXml/data) - string-length('World') +1              ) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer MethodInfo mi = typeof(MyType).GetMethod('methodname'); Assert.IsFalse (Attribute.IsDefined (mi, typeof(MyAttributeClass))); May 11, 2026 at 8:20 am
  • added an answer <%=Html.RouteLink(Model.Name, 'SomeRoute', New With {.id = Model.Id})%> May 11, 2026 at 8:20 am
  • added an answer You have to 'clear' your floats. Floating elements takes them… May 11, 2026 at 8:20 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Related Questions

Loading...

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.