【问题标题】:Retrieving Inner Text of Html Tag C#检索 Html 标记 C# 的内部文本
【发布时间】:2011-11-07 03:13:23
【问题描述】:

我有一个包含 html 的字符串。在这个字符串里面有一个 html 标签,我想检索它的内部文本。如何在 C# 中做到这一点?

这是我要检索其内部文本的 html 标记:

<td width="100%" class="container">

【问题讨论】:

标签: c# innertext


【解决方案1】:

使用Html Agility Pack


编辑类似这样的东西(未测试)

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
}

你也可以直接用a different XPath selector选择文字。


相关问题:

【讨论】:

  • 你能从包含 html 的字符串中加载一个 html 文档吗?还是我必须给它一条路径?
  • 回答了我自己的问题:而不是使用 Load 使用 LoadHtml intead。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2020-01-19
  • 1970-01-01
相关资源
最近更新 更多