C# 在HtmlAgilityPack、Xpath中使用谓词

C# 在HtmlAgilityPack、Xpath中使用谓词,c#,asp.net,xpath,html-agility-pack,C#,Asp.net,Xpath,Html Agility Pack,我想从网站上获取数据。我正在使用HtmlAgilityPack(C#)。网站中的内容是这样的 <div id="list"> <div class="list1"> <a href="example1.com" class="href1" >A1</a> <a href="example4.com" class="href2" /> </div> <div class="list2">

我想从网站上获取数据。我正在使用HtmlAgilityPack(C#)。网站中的内容是这样的

<div id="list">
  <div class="list1">
    <a href="example1.com" class="href1" >A1</a>
    <a href="example4.com" class="href2" />
  </div>
  <div class="list2">
   <a href="example2.com" class="href1" >A2</a>
   <a href="example5.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example3.com" class="href1" >A3</a>
   <a href="example6.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example4.com" class="href1" >A4</a>
   <a href="example6.com" class="href2" />
  </div>
  <div class="list3">
   <a href="example5.com" class="href1" >A5</a>
   <a href="example6.com" class="href2" />
  </div><div class="list3">
   <a href="example6.com" class="href1" >A6</a>
   <a href="example6.com" class="href2" />
  </div><div class="list3">
   <a href="example3.com" class="href1" >A7</a>
   <a href="example6.com" class="href2" />
  </div>
</div>


在这里,我们有7个class=“href1”链接。我只想获取3个链接(从第3个链接到第5个链接)。如何获取这些特定链接?

您的数据似乎已经是格式良好的XML。如果您正在解析XHTML页面,那么您可能不必使用.NETFramework的
System.Xml
类。例如,要将数据加载到
XElement
,可以使用:

XElement xElement = XElement.Parse(@"
    <div id=""list"">
        <div class=""list1"">
            <a href=""example1.com"" class=""href1"" >A1</a>
            <a href=""example4.com"" class=""href2"" />
        </div>
        <div class=""list2"">
            <a href=""example2.com"" class=""href1"" >A2</a>
            <a href=""example5.com"" class=""href2"" />
        </div>
        <div class=""list3"">
            <a href=""example3.com"" class=""href1"" >A3</a>
            <a href=""example6.com"" class=""href2"" />
        </div>
        <div class=""list3"">
            <a href=""example4.com"" class=""href1"" >A4</a>
            <a href=""example6.com"" class=""href2"" />
        </div>
        <div class=""list3"">
            <a href=""example5.com"" class=""href1"" >A5</a>
            <a href=""example6.com"" class=""href2"" />
        </div>
        <div class=""list3"">
            <a href=""example6.com"" class=""href1"" >A6</a>
            <a href=""example6.com"" class=""href2"" />
        </div>
        <div class=""list3"">
            <a href=""example3.com"" class=""href1"" >A7</a>
            <a href=""example6.com"" class=""href2"" />
        </div>
    </div>");
另一方面,如果您有一个
HtmlAgilityPack.HtmlDocument
实例,则可以使用以下命令执行XPath查询:

HtmlNodeCollection links = htmlDoc.DocumentNode.SelectNodes("//a[@class='href1']");
var links3to5 = links.Cast<HtmlNode>().Skip(2).Take(3).ToList();
HtmlNodeCollection links=htmlDoc.DocumentNode.SelectNodes(//a[@class='href1']);
var links3to5=links.Cast().Skip(2.Take(3.ToList));
此类代码:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(myHtmlFile);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes(
        "//div[@class='list3' and position() > 2 and position() < 6]/a[@class='href1']"))
    {
        Console.WriteLine("node:" + node.InnerText);
    }
    HtmlDocument doc = new HtmlDocument();
    doc.Load(myHtmlFile);
    foreach (HtmlNode node in doc.DocumentNode.SelectNodes(
        "//div[@class='list3' and position() > 2 and position() < 6]/a[@class='href1']"))
    {
        Console.WriteLine("node:" + node.InnerText);
    }
node:A3
node:A4
node:A5