Remove attributes using HtmlAgilityPack

六月ゝ 毕业季﹏ 提交于 2019-11-30 17:09:26

Your code snippet seems to be correct - it removes the attributes. The thing is, DocumentNode .InnerHtml(I assume you monitored this property) is a complex property, maybe it get updated after some unknown circumstances and you actually shouldn't use this property to get the document as a string. Instead of it HtmlDocument.Save method for this:

string result = null;
using (StringWriter writer = new StringWriter())
{
    htmlDoc.Save(writer);
    result = writer.ToString();
}

now result variable holds the string representation of your document.

One more thing: your code may be improved by changing your expression to "//*[@style]" which gets you only elements with style attribute.

Here is a very simple solution

VB.net

element.Attributes.Remove(element.Attributes("style"))

c#

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