how do i get the text from a nested

tag in an external html using html agility pack?

c# html html-agility-pack xpath

Question

I am trying to get some text from an external site. The text I am trying to get is nested in a paragraph tag. The div has has a class value

html code snippet:

<div class="discription"><p>this is the text I want to grab</p></div>

current c# code:

public String getDiscription(string url)
{
    var web = new HtmlWeb();
    var doc = web.Load(url);


    var nodes = doc.DocumentNode.SelectNodes("//div[@class='discription']");

    if (nodes != null)
    {
        foreach (var node in nodes)
        {
            string Description = node.InnerHtml;
            return Description;
        }
    } else
      {
       string error = "could not find text";
       return error;
      }
}

what I dont understand is the syntax of the xpath //div[@class='discription'] I know it is wrong what should the xpath be?

Accepted Answer

use //div[@class='discription']/p.

Breakdown:

//div                    - All div elements
[@class='discription']   - With a class attribute whose value is discription
/p                       - Select the child p elements


Related

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