Html Agilitypack 获取外部物品
Html Agilitypack Getting Outer Items
我需要通过 id="duyuru3483"
获得 <div class="month">May 2015</div>
和 <div class="author">Alex Xander</div>
。我如何到达上层 div(XPATH 代码):
<div class="item">
<div class="dateBox">
<div class="month">May 2015</div>
<div class="day">07</div>
</div>
<div class="mainInfo">
<div class="title">
<a href="#" class="text-justify" data-toggle="modal" data-target="#duyuru3483">
2014-2015 Yılı Mezuniyet Töreni Hk.
</a>
<div class="modal fade" id="duyuru3483" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="background-color: #009E49;">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">
<div class="duyuruBaslik">2014-2015 Yılı Mezuniyet Töreni Hk.</div>
</h4>
</div>
<div class="modal-body">
<div class="duyuruMetni"> Mezuniyet törenine katılmak için yapılmasıgereken işlemler ve doldurulması gereken form ektedir. </div>
<hr>
<div class="author" > Ek : <a style="color:##428bca" href="../files/19_mezuniyet.rar">indir</a> </div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Kapat</button>
</div>
</div>
</div>
</div>
</div>
<div class="author">Alex Xander</div>
一种选择是使用 Ancestors
:
var modal = doc.GetElementbyId("duyuru3483");
var parent = modal.Ancestors()
.Single(e => e.Attributes["class"] !=null &&
e.Attributes["class"].Value == "item");
另一种选择是使用 XPath:
var parent = doc.DocumentNode
.SelectSingleNode("div[@class='item'][//div[@id='duyuru3483']]");
(您可以使用以下算法直接进入 .dateBox
:
div[@class='item'][//div[@id='duyuru3483']]/div[@class='dateBox']
而且,当然,您可以只使用 ParentNode
,但这不太稳健:
var parent = modal.ParentNode.ParentNode.ParentNode;
所有这些都会让您到达 .item
- 从那里获取数据应该很容易。
我需要通过 id="duyuru3483"
获得 <div class="month">May 2015</div>
和 <div class="author">Alex Xander</div>
。我如何到达上层 div(XPATH 代码):
<div class="item">
<div class="dateBox">
<div class="month">May 2015</div>
<div class="day">07</div>
</div>
<div class="mainInfo">
<div class="title">
<a href="#" class="text-justify" data-toggle="modal" data-target="#duyuru3483">
2014-2015 Yılı Mezuniyet Töreni Hk.
</a>
<div class="modal fade" id="duyuru3483" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="background-color: #009E49;">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">
<div class="duyuruBaslik">2014-2015 Yılı Mezuniyet Töreni Hk.</div>
</h4>
</div>
<div class="modal-body">
<div class="duyuruMetni"> Mezuniyet törenine katılmak için yapılmasıgereken işlemler ve doldurulması gereken form ektedir. </div>
<hr>
<div class="author" > Ek : <a style="color:##428bca" href="../files/19_mezuniyet.rar">indir</a> </div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Kapat</button>
</div>
</div>
</div>
</div>
</div>
<div class="author">Alex Xander</div>
一种选择是使用 Ancestors
:
var modal = doc.GetElementbyId("duyuru3483");
var parent = modal.Ancestors()
.Single(e => e.Attributes["class"] !=null &&
e.Attributes["class"].Value == "item");
另一种选择是使用 XPath:
var parent = doc.DocumentNode
.SelectSingleNode("div[@class='item'][//div[@id='duyuru3483']]");
(您可以使用以下算法直接进入 .dateBox
:
div[@class='item'][//div[@id='duyuru3483']]/div[@class='dateBox']
而且,当然,您可以只使用 ParentNode
,但这不太稳健:
var parent = modal.ParentNode.ParentNode.ParentNode;
所有这些都会让您到达 .item
- 从那里获取数据应该很容易。