HTML Agility Pack Parsing div

c# html-agility-pack parsing

Question

I'm trying to parse HTML, I need to get "text" from this part:

<div class="_gdf kno-fb-ctx">
    <span data-ved="0ahUKEwjIr9brjO7UAhUnYZoKHda-ALgQ2koIogEoAjAT"> text</span>
</div>

Here's my C# code:

var message = doc.DocumentNode.SelectSingleNode("//div[@class='_gdf kno-fb-ctx']").InnerText;
Console.WriteLine(message);

What I'm doing wrong ?

Popular Answer

I see that you are not selecting the actual 'Span' node to read the InnertTex. You have selected div and trying to read InnertTex, which won't give you desired result "Text". Instead you can do like below:

        HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml("<div class='_gdf kno-fb-ctx'><span data-ved = '0ahUKEwjIr9brjO7UAhUnYZoKHda-ALgQ2koIogEoAjAT'> text </span ></div >");
        var text = doc.DocumentNode.SelectSingleNode("//div[@class=\"_gdf kno-fb-ctx\"]//span").InnerText;



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