Cameron Barge
Cameron Barge

Reputation: 309

SelectSingleNode Html Document

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"?

Upvotes: 1

Views: 3410

Answers (3)

Cameron Barge
Cameron Barge

Reputation: 309

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!

Upvotes: -1

Furqan Safdar
Furqan Safdar

Reputation: 16708

Make sure the span element with title attribute exists with value as 'input' in your HtmlDocument object of HtmlAgilityPack.

For proper checking, try this piece of code:

if (doc.DocumentNode != null)
{
    var span = doc.DocumentNode.SelectSingleNode("//span[@title='input']");

    if (span != null)
        return span.InnerText;
}

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262989

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.

Upvotes: 2

Related Questions