使用HTML Agility Pack在HtmlNodeCollection中选择节点

时间:2014-02-19 19:24:40

标签: c# linq xml-parsing html-agility-pack xmlnode

我有这样的html结构:

<div class="class1">
    <div class="class2">
        <a href="test" title="professional and enthusiast programmers">Stack Overflow</a>
    </div>
    <div class="class2">
    </div>
    <div class="class2">
    </div>
    <div class="class2">
    </div>
</div>
<div class="class1">
    <div class="class2">
        <a href="test" title="professional and enthusiast programmers">Stack Overflow</a>
    </div>
</div>

我试图用class1选择所有节点:

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//*[contains(@class,' class1 ')]");

并选择该节点内包含class2的所有节点:

foreach (HtmlNode node in nodes)
{
   HtmlNodeCollection nodes2 = node.SelectNodes("//*[contains(@class,' class2 ')]");
}

但nodes2从所有节点获取class2。我做错了什么?

2 个答案:

答案 0 :(得分:4)

var nodes = doc.DocumentNode.SelectNodes("//div[@class='class1']");

var nodes2 = nodes.Select(c1 => c1.SelectNodes("div[@class='class2']"));

答案 1 :(得分:0)

替代XPath为我工作

var nodes = doc.DocumentNode.SelectNodes("//div[@class='class1']");
var nodes2 = nodes.Select(c1 => c1.SelectNodes(".//div[@class='class2']"));