【问题标题】:Using HtmlAgilityPack with c# to Find Nested repeated class name section使用带有 c# 的 HtmlAgilityPack 来查找嵌套的重复类名部分
【发布时间】:2013-03-02 17:30:11
【问题描述】:

我无法弄清楚查询(XPath?)是如何访问嵌套元素的,如下所示。我想让“200”超出范围。我确定的事情是 out 类是“top2 bigone”是一个 div,我希望跨度与类“top2 bigone”一起。我把我的 C# 放在下面那当然不行。

谢谢

<html>
<body>
    <div class='top1 bigone'>
        <span class='counts numbers'>100</span>
    </div>

    <div class='top2 bigone'>
        <span class='counts numbers'>200</span>
    </div>
</body>
</html>

我的破代码:

var input4 =
            (from inputx in htmlDoc.DocumentNode.Descendants("top2 bigone")
             where inputx.Attributes.Count > 0 && inputx.Attributes["class"].Value == "counts numbers"
             select inputx).FirstOrDefault();

【问题讨论】:

    标签: c# html-parsing html-agility-pack


    【解决方案1】:

    试试这个,经过测试和工作:

    HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class = 'top2 bigone']//span[@class = 'counts numbers']");
    
    if (node != null)
    {
        string number = node.InnerText; // 200
    }
    else
    {
         MessageBox.Show("node = null");
    }
    

    【讨论】:

    • 谢谢,等我回到我的真实电脑上试试看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多