2

Yesterday I posted this question :

https://stackoverflow.com/questions/56182368/html-agility-pack-select-node-after-particular-paragraph

I resolved using the solution : htmlSnippet.DocumentNode.SelectNodes('//p[text()="targetWord"]/following-sibling::ul[1]//li')

Today on another project with same identical configuratione and HTML agility pack version I obtain the error message in title. What can I do to solve it?

System.Xml.XPath.XPathException: 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.'

Code that throws the exception:

var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmlText);


            //var ulCollection = htmlDoc.DocumentNode.SelectNodes("//ul");

            var tasksNodesCollection = htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]/following-sibling::ul[1]//li");
nellowl
  • 339
  • 3
  • 16
  • Might find the solution here "https://stackoverflow.com/questions/13511527/namespace-manager-or-xsltcontext-needed-this-query-has-a-prefix-variable-or-u" – Test12345 May 18 '19 at 19:02
  • How this applies to html agility pack? – nellowl May 18 '19 at 19:14
  • Can you provide the code where the error is coming – Test12345 May 18 '19 at 19:19
  • You need to post the response. It looks like the response is XML and you cannot create a document with the response xml – jdweng May 18 '19 at 20:48
  • var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlText); //var ulCollection = htmlDoc.DocumentNode.SelectNodes("//ul"); var tasksNodesCollection = htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]/following-sibling::ul[1]//li"); – nellowl May 18 '19 at 21:37
  • I added the code segment that throws the exception in the question – nellowl May 18 '19 at 21:38
  • Solved!!! I used text()= instead of textvalue()= I have no idea why yesterday it worked!!! – nellowl May 19 '19 at 09:50

2 Answers2

1

You have a colon in the text you are looking for.

htmlDoc.DocumentNode.SelectNodes("//p[TextValue()=\"Paragraph:\"]

The parser thinks it's an XML namespace qualifier. Use the escape form " for the colon.

rburte
  • 627
  • 6
  • 11
  • I have the problem also deleting the colon – nellowl May 19 '19 at 09:23
  • //p/following-sibling::ul[1]//li expression works. Problem is when I add textvalue also without semicolon. maybe that I escape wrong the double quotes in the c# string? – nellowl May 19 '19 at 09:40
  • 1
    Solved!!! I used text()= instead of textvalue()= I have no idea why yesterday it worked!!! – nellowl May 19 '19 at 09:50
0

Solved!!! I used text()= instead of textvalue()= I have no idea why yesterday it worked!!!

nellowl
  • 339
  • 3
  • 16