Ric*_*ick 3 c# html-agility-pack
我一直试图从DIV元素的样式属性中删除样式定义。HTML代码:
<div class="el1" style="width:800px; max-width:100%" />
...
<div class="el2" style="width:800px; max-width:100%" />
Run Code Online (Sandbox Code Playgroud)
我可能需要对这些元素中的多个元素进行操作。
到目前为止,这是我使用HtmlAgilityPack所获得的。
foreach (HtmlNode div in doc.DocumentNode.SelectNodes("//div[@style]"))
{
if (div != null)
{
div.Attributes["style"].Value["max-width"].Remove(); //Remove() does not appear to be a function
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是选择任何具有样式属性的东西。查找最大宽度定义并将其删除。
关于如何实现这一目标的任何指导?
感谢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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1739 次 |
| 最近记录: |