【发布时间】:2012-11-06 15:31:45
【问题描述】:
我用下面的方法来提取html表单中的文本:
public string getAllText(string _html)
{
string _allText = "";
try
{
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(_html);
var root = document.DocumentNode;
var sb = new StringBuilder();
foreach (var node in root.DescendantNodesAndSelf())
{
if (!node.HasChildNodes)
{
string text = node.InnerText;
if (!string.IsNullOrEmpty(text))
sb.AppendLine(text.Trim());
}
}
_allText = sb.ToString();
}
catch (Exception)
{
}
_allText = System.Web.HttpUtility.HtmlDecode(_allText);
return _allText;
}
问题是我也得到了脚本和样式标签。
我怎样才能排除它们?
【问题讨论】:
-
内联样式,即
怎么样?我在 OuterHtml 中看到它,但也想去掉所有内联样式。
-
if (childNode.Attributes.Contains("style")) { childNode.Attributes.Remove("style"); } if (childNode.Attributes.Contains("class")) { childNode.Attributes.Remove("class"); }
标签: c# html-agility-pack