HtmlAgilityPack编程 - HtmlDocument删除html的样式
HtmlAgilityPack编程 - HtmlDocument删除html的样式
C# 全选
/// <summary>
/// 删除样式(style)
/// </summary>
/// <param name="doc">HtmlDocument对象</param>
/// <param name="elementName">元素标记,img/p/div</param>
/// <param name="styleName">样式名称,font-size,display</param>
protected void DoRemoveStyle(HtmlAgilityPack.HtmlDocument doc, string elementName, string styleName)
{
var list = doc.DocumentNode.SelectNodes("//" + elementName);//img/p/div
if (list != null && list.Count > 0)
{
styleName = styleName + ":";//样式标识添加冒号,font-size:
foreach (HtmlNode node in list)
{
var styles = node.GetAttributeValue("style", "");//获取样式
//获取数组;分开
var ss = styles.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
//组合样式
StringBuilder sb = new StringBuilder();
foreach (var s in ss)
{
if (s.Replace(" ", "").ToLower().Contains(styleName.ToLower()))
continue;
else
sb.Append(s + ";");
}
//设置新样式
if (sb.Length > 0)
node.SetAttributeValue("style", sb.ToString());
}
}
}
CSCODE.NET - C/S架构Winform开发框架文库
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网