Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Inner Text of Html Tag C#

Tags:

c#

innertext

I have a string that contains html. Inside of this string there is an html tag and I want to retrieve the inner text of that. How can I do that in C#?

Here is the html tag whose inner text I want to retrieve:

<td width="100%" class="container">
like image 727
John Dougherty Avatar asked Oct 17 '25 19:10

John Dougherty


1 Answers

Use the Html Agility Pack.


Edit something like this (not tested)

HtmlDocument doc = new HtmlDocument();
string html = /* whatever */;
doc.LoadHtml(html);
foreach(HtmlNode td in doc.DocumentElement.SelectNodes("//td[@class='container']")
{
    string text = td.InnerText;
    // do whatever with text
}

You can also select the text directly with a different XPath selector.


Related questions:

  • How to use HTML Agility pack
  • HTMLAgilityPack parse in the InnerHTML
  • C#: HtmlAgilityPack extract inner text
like image 93
Matt Ball Avatar answered Oct 20 '25 10:10

Matt Ball