【问题标题】:How to get only div with not style attribute?如何仅获取没有样式属性的 div?
【发布时间】:2019-04-10 09:46:29
【问题描述】:

假设我有以下 html 结构:

<div class="table-container"></div>
<div class="table-container" style="display: none;"></div>
<div class="table-container" style="display: none;"></div>
<div class="table-container"></div>

我怎样才能只获得没有style 属性的div?我这样做了:

HtmlNodeCollection containers = doc.DocumentNode.SelectNodes("//div[@class='table-container']");

有一个属性可以让我这样做吗?

【问题讨论】:

  • //div[@class='table-container' and not(@style)] ?
  • @splash58 不知道,谢谢

标签: c# xpath html-agility-pack


【解决方案1】:

你已经接近了。只需添加Where

var nodes = doc
   .DocumentNode
   .ChildNodes
   .Where(n => n.Attributes.Count == 1 && 
               n.Attributes[0].Name == "class")
   .ToList();

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 2016-05-19
    • 1970-01-01
    • 2010-12-30
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多