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

  • Home
  • SEARCH
  • 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 63k
  • Answers 63k
  • 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 Probably your best way is to put the passwords in… May 11, 2026 at 10:29 am
  • added an answer Create a new 'Settings' file to add a complex/user-defined type… May 11, 2026 at 10:29 am
  • added an answer It depends on the amount of data - the best… May 11, 2026 at 10:29 am

Related Questions

My current program need to use programatically create a XPathExpression instance to apply to
My current development project has two aspects to it. First, there is a public
My current preferred C++ environment is the free and largely excellent Microsoft Visual Studio
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current view is no, prefer Transact SQL stored procedures because they are a
My current setup.py script works okay, but it installs tvnamer.py (the tool) as tvnamer.py
My current place of employment is currently in a transition, new ownership has taken
My current project uses NUnit for unit tests and to drive UATs written with
My current position is this: if I thoroughly test my ASP.NET applications using web
My current app needs to store address information for a user. I'm currently debating

Trending Tags

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

Top Members

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

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.