C# 删除样式元素内的宽度等属性
我需要删除样式元素内部的宽度,使用HTML敏捷包pref 我尝试使用:C# 删除样式元素内的宽度等属性,c#,html-agility-pack,C#,Html Agility Pack,我需要删除样式元素内部的宽度,使用HTML敏捷包pref 我尝试使用: public static void RemoveStyleAttributes(this HtmlDocument html) { var elementsWithStyleAttribute = html.DocumentNode.SelectNodes("//@style"); if (elementsWithStyleAttribute!=null) { foreach (var el
public static void RemoveStyleAttributes(this HtmlDocument html)
{
var elementsWithStyleAttribute = html.DocumentNode.SelectNodes("//@style");
if (elementsWithStyleAttribute!=null)
{
foreach (var element in elementsWithStyleAttribute)
{
element.Attributes["style"].Remove();
}
}
}
但是无法针对style中的单个元素(因为我不想完全删除style)您可以使用
element.attribute[“style”].Value=yourStringOperationFunction(element.attribute[“style”].Value)更新style属性代码>操作字符串的函数,您必须自己构建,或者需要从可以解析CSS的库中获取。AgilityPack不包含此类函数。这个可能适合您:width\s*\:.+?(?=\;)\s*代码>将其与RegEx.Replace(string,string)一起使用,否则如果变得复杂,请按照jess的建议查找库