Using html agility pack, how can I obtain the text from a nested p> element in an external html?

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?

1
0
4/15/2012 8:13:24 PM

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
0
4/15/2012 8:15:56 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow