Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Agility Pack Find specific span

Tags:

c#

I would like to find a span with a specific Id and retrieve the inner text. But I can't seem to find the way to do that.

can someone guide me with this

like image 569
Killercode Avatar asked Dec 07 '22 19:12

Killercode


2 Answers

You may try something along the lines:

var doc = new HtmlDocument();
doc.Load("foo.html");
var node = doc.DocumentNode.SelectSingleNode("//span[@id='foo']");
if (node != null)
{
    var innerText = node.InnerText;
}
like image 198
Darin Dimitrov Avatar answered Dec 09 '22 08:12

Darin Dimitrov


I personally prefer to turn the whole thing into an XElement and query it that way when using Html Agility. Easier than xpath IMHO. But Darin's answer works

like image 43
kmcc049 Avatar answered Dec 09 '22 07:12

kmcc049