C# Htmlagibility从Div节点的样式参数中删除属性
我一直在尝试从DIV元素的style属性中删除样式定义。 HTML代码:C# Htmlagibility从Div节点的样式参数中删除属性,c#,html-agility-pack,C#,Html Agility Pack,我一直在尝试从DIV元素的style属性中删除样式定义。 HTML代码: <div class="el1" style="width:800px; max-width:100%" /> ... <div class="el2" style="width:800px; max-width:100%" /> 我的思考过程是选择任何具有样式属性的。查找最大宽度定义并将其删除 关于如何实现这一点,有什么指导吗?感谢Marcel为我指出了正确的方向: 这是对我有效的解决方案 H
<div class="el1" style="width:800px; max-width:100%" />
...
<div class="el2" style="width:800px; max-width:100%" />
我的思考过程是选择任何具有样式属性的。查找最大宽度定义并将其删除
关于如何实现这一点,有什么指导吗?感谢Marcel为我指出了正确的方向: 这是对我有效的解决方案
HtmlNodeCollection divs = doc.DocumentNode.SelectNodes("//div[@style]");
if (divs != null)
{
foreach (HtmlNode div in divs)
{
string style = div.Attributes["style"].Value;
string pattern = @"max-width(.*?)(;)";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
string newStyle = regex.Replace(style, String.Empty);
div.Attributes["style"].Value = newStyle;
}
}
你不能那样做。您只能获取
样式
值。从那以后,你可以使用reg-ex或其他恶魔装置删除最大宽度
,最后将其设置回属性。非常感谢你的回答,效果非常好!为什么不把自己的答案标记为已接受的答案呢?我使用这种方法从td标签中删除所有高度样式,但不从tr标签中删除。工作得很卖力!
HtmlNodeCollection divs = doc.DocumentNode.SelectNodes("//div[@style]");
if (divs != null)
{
foreach (HtmlNode div in divs)
{
string style = div.Attributes["style"].Value;
string pattern = @"max-width(.*?)(;)";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
string newStyle = regex.Replace(style, String.Empty);
div.Attributes["style"].Value = newStyle;
}
}