【问题标题】:Remove all classes and ids from parsed HTML with HtmlAgilityPack使用 HtmlAgilityPack 从解析的 HTML 中删除所有类和 id
【发布时间】:2015-05-21 16:05:33
【问题描述】:
我使用 HtmlAgilityPack 来解析一些 html 页面,我从这个页面中提取 html 标签,如下所示:
HtmlNode bodyContent = document.DocumentNode.SelectSingleNode("//body");
var all_text = bodyContent.SelectNodes("//div | //ul | //p | //table");
在返回的 html 中,每个标签都包含 class 和 id,我想删除所有 id-s 和所有 class 我该怎么做?
【问题讨论】:
标签:
c#
html
html-agility-pack
【解决方案1】:
也许你应该检查这个链接:link。
尽我所能,告诉当你有 HtmlNode 时你可以使用它的属性 Attributes。此集合具有方法 Remove(string) 接收要删除的属性名称。好吧,我在一个小项目中这样使用它。我不确定这是否对您有帮助。
所以基本上:
HtmlNode bodyContent = document.DocumentNode.SelectSingleNode("//body");
var all_text = bodyContent.SelectNodes("//div | //ul | //p | //table");
foreach(var node in all_text)
{
node.Attributes.Remove("class");
node.Attributes.Remove("id");
}