如何使用C#删除HTML中的特定样式标签 [英] How to remove specific style tag in html using C#
本文介绍了如何使用C#删除HTML中的特定样式标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的html代码:
Here is my html code:
<p><span style="background:lime;Color:Red;">Contrary to popular belief, <b><u>Lorem Ipsum is not simply</u></b> random text. It has roots in a piece of classical Latin literature from <span style="background:blue;">45 BC, making it over 2000 years</span> old. Richard McClintock, </span><b>
从上面的代码中,我需要删除背景属性&使用来自所有跨度的C#的值。样式标签中的其他值应保留。例如:
From above code, i need to remove the background attributes & value using C# from all the spans. The other values in style tag should remain. Eg:
<span style="background:lime;Color:Red;">Contrary to popular belief,.....</span>
应看
should look
<span style="Color:Red;">Contrary to popular belief,.....</span>
请帮助...!
推荐答案
string html = @"<span style=""background:lime;Color:Red;"">Contrary to popular belief,.....</span>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
foreach (var span in doc.DocumentNode.Descendants("span"))
{
var style = span.Attributes["style"].Value;
span.Attributes["style"].Value = String.Join(";", style.Split(';').Where(s => !s.ToLower().Trim().StartsWith("background:")));
}
var newHtml = doc.DocumentNode.InnerHtml;
这篇关于如何使用C#删除HTML中的特定样式标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文