【问题标题】:HTML agility pack first specific childnodesHTML 敏捷包第一个特定的子节点
【发布时间】:2015-09-29 13:52:09
【问题描述】:
<html>
<body>
    <p> This 
        <p> should not be displayed twice</p>
    </p>
    <a href="http://www.w3schools.com">Visit W3Schools.com!</a> 
    <div> Do not enter</div>
    <p> Gibberish </p>
</body>
</html>

所以我想访问 body-tag 的某些第一个子节点。在这种情况下,我只想要 p-tag 和 a-tag。

当前代码:

foreach(HtmlNode h in body.Elements("p"))
{
    if (h.Name == "p"){
        //Do something
    }
    if (h.Name == "a"){
       //Do something else
    }
}

显然这不起作用,因为我只从 body-tag 中获取 p-tags。但是,是否有一些很棒的 xpath 代码也可以让我获得 a-tags。

【问题讨论】:

    标签: filter nodes html-agility-pack child-nodes


    【解决方案1】:

    您可以在 xpath 中使用“或”(|)。所以你会看起来像这样:

    String xPath = "p|a";
    HtmlNodeCollection bodyDecendants = bodyNode.SelectNodes(xPath);
    

    【讨论】:

    • 已经试过了。那没有用。就像 HAP 不完全支持 Xpaths。但显然它与 SelectNodes 完美配合。
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多