简体   繁体   English

HtmlAgility从Div节点的样式参数中删除属性

[英]HtmlAgility remove attribute from style parameter of Div node

I am stuck trying to remove a style definition from the style attribute of a DIV element. 我试图从DIV元素的style属性中删除样式定义。 HTML code: HTML代码:

<div class="el1" style="width:800px; max-width:100%" />
...

<div class="el2" style="width:800px; max-width:100%" />

There may be more than 1 of these elements that I need to apply the manipulation to. 我需要将这些元素中的一个以上元素应用于操作。

Here is what I have thus far using HtmlAgilityPack. 这是我到目前为止使用的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
  }
 }

My thought process was to select any with a style attribute. 我的思维过程是选择任何一个样式属性。 Look for a max-width definition and remove it. 查找最大宽度定义并将其删除。

Any guidance on how this can be achieved? 有关如何实现这一目标的任何指导?

THanks Marcel for pointing me to the right direction: 谢谢马塞尔指出我正确的方向:

Here is the solution that worked for me. 这是适合我的解决方案。

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;
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM