SelectSingleNode Html Document

c# html-agility-pack nullreferenceexception

Question

Currently I am dealing with an HtmlDocument in c# from a website:

return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText;

I want to get the inner text from a span with the title "input". Above is my current code but I receive a NullReferenceException when trying to run it. What should my implicit parameter be in order to retrieve the text from "input"?

Accepted Answer

return doc.DocumentNode.SelectSingleNode("//span[@title='"+input+"']").InnerText;

Because input is not a string, it has to be concatenated to fit the parameters. Thanks for all of you help!


Popular Answer

You have to delimit strings with quotes in XPath expressions:

return doc.DocumentNode.SelectSingleNode("//span[@title='input']").InnerText;

Plain input will try to match a node by that name and substitute its value.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why