在 C# 中循环遍历 HTMLAgilityPack 中的单个节点

Looping through single nodes in HTMLAgilityPack in C#

我正在尝试从具有以下结构的项目列表中获取 'ID' 和 'URL':

 <li class="ad container">
                   <article class="item_list view_listing " data-id="109199375"> <div class="item_list_section item_date"> <time datetime="2015-06-21 20:10:05"> 21 giu<br> 20:10 </time>  </div> <div class="item_list_section item_image">  <a href="http://www.subito.it/hobby-collezionismo/elicottero-scorpio-210-treviso-109199375.htm" >  <div class="item_image_wrapper">   <img src="http://s.sbito.it/bigthumbs/07/07201887042182.jpg">   <div class="item_extra_images">  <img src="http://s.sbito.it/1201434547094/img2/ico_photo.png"> <span class="item_extra_photo_number">3</span> </div>   </div>  </a>  </div> <div class="item_list_section item_description"> <h2>  <a href="http://www.subito.it/hobby-collezionismo/elicottero-scorpio-210-treviso-109199375.htm" name="109199375" title="Elicottero scorpio 2&10" >  Elicottero scorpio 2&10  </a>  </h2>  <span class="item_price">35 &euro;</span>     <span class="item_category">Collezionismo</span>  </div>  <div class="item_list_section item_info">  <span class="item_type">In vendita</span>    <span class="item_location">Colle Umberto <em class="item_city">(TV)</em></span> </div>  </article>
        </li>

   <li class="ad container">
                   <article class="item_list view_listing " data-id="128462782"> <div class="item_list_section item_date"> <time datetime="2015-06-20 18:54:44"> 20 giu<br> 18:54 </time>  </div> <div class="item_list_section item_image">  <a href="http://www.subito.it/hobby-collezionismo/elicottero-costruzione-tedesca-verona-128462782.htm" >  <div class="item_image_wrapper">   <img src="http://s.sbito.it/bigthumbs/06/06201423464701.jpg">    </div>  </a>  </div> <div class="item_list_section item_description"> <h2>  <a href="http://www.subito.it/hobby-collezionismo/elicottero-costruzione-tedesca-verona-128462782.htm" name="128462782" title="Elicottero costruzione tedesca" >  Elicottero costruzione tedesca  </a>  </h2>  <span class="item_price">170 &euro;</span>     <span class="item_category">Collezionismo</span>  </div>  <div class="item_list_section item_info">  <span class="item_type">In vendita</span>    <span class="item_location">Legnago <em class="item_city">(VR)</em></span> </div>  </article>
        </li>

    <li class="ad container">
                   <article class="item_list view_listing " data-id="128437705"> <div class="item_list_section item_date"> <time datetime="2015-06-20 14:50:00"> 20 giu<br> 14:50 </time>  </div> <div class="item_list_section item_image">  <a href="http://www.subito.it/hobby-collezionismo/elicottero-rc-450-verona-128437705.htm" >  <div class="item_image_wrapper">   <img src="http://s.sbito.it/bigthumbs/06/06201463968327.jpg">   <div class="item_extra_images">  <img src="http://s.sbito.it/1201434547094/img2/ico_photo.png"> <span class="item_extra_photo_number">2</span> </div>   </div>  </a>  </div> <div class="item_list_section item_description"> <h2>  <a href="http://www.subito.it/hobby-collezionismo/elicottero-rc-450-verona-128437705.htm" name="128437705" title="Elicottero rc 450" >  Elicottero rc 450  </a>  </h2>  <span class="item_price">200 &euro;</span>     <span class="item_category">Collezionismo</span>  </div>  <div class="item_list_section item_info">  <span class="item_type">In vendita</span>    <span class="item_location">Veronella <em class="item_city">(VR)</em></span> </div>  </article>
        </li>

问题是我想将它们添加到列表中,但我不能执行多个循环,否则它们将位于不同的索引中。这就是我想要做的:

            var list = page.DocumentNode.SelectNodes("//li[@class='ad container']");


            foreach (var obj in list)
            {

            items.Add( new Item() { itemUrl = obj.SelectSingleNode("/a").Attributes["href"].Value, itemId = obj.SelectSingleNode("//article").Attributes["data-id"].Value });

            }

但它不起作用,例如对于 "data-id" 它重复它找到的第一个元素。我如何让它发挥作用?

谢谢。

使用相对路径itemUrl = obj.SelectSingleNode(".//a").Attributes["href"].Value, itemId = obj.SelectSingleNode(".//article").Attributes["data-id"].Value