Retrieving Inner Text of Html Tag C#

眉间皱痕 提交于 2020-01-11 10:14:26

问题


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">

回答1:


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


来源:https://stackoverflow.com/questions/7249254/retrieving-inner-text-of-html-tag-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!