HtmlAgilityPack - selecting single node from a node collection using XPath
i have been fighting with this code for a few hours now....
Sample Node for results:
<div class="left vcard" sizcache="1" sizset="32">
<h2 class="clearfix fn org url" sizcache="1" sizset="32">
<a id="listItemTitle_11310540" href="/marcali/viragok-viragkuldes/11310540/ANDOK_MATYAS/hirdetes.mtt">ANDÓK MÁTYÁS</a> <a class="removeFromList" href="#">törlés</a>
</h2>
<p class="description">
2009 decemberében nyitottuk meg az Ezerszirom Virágbolt-ot Marcaliban a Petőfi Sándor u.11-ben. Szeretettel várja a kedves vásárlókat Horváth Györgyike virágkötő, aki 15 éve kápráztatja el kreatíva...<开发者_如何学Go/p>
<ul class="profession" sizcache="1" sizset="34">
<li sizcache="1" sizset="34"><a href="/szakmak/viragok-viragkuldes/index.mtt">Virágok, virágküldés</a> </li>
</ul>
<div class="clearfix margined" sizcache="1" sizset="35">
<p class="address adr">
<span>Cím:</span> 8700 Marcali, Petőfi S. utca 11 .</p>
<ul class="nav clearfix" sizcache="1" sizset="35">
<li class="mapLink" sizcache="1" sizset="35"><span>-</span><a class="mapLink" href="/terkep/11310540/ANDOK_MATYAS">térképen mutat</a> </li>
<li class="routeplanner" sizcache="1" sizset="36"><em>útvonaltervezés</em> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&routePlanCount=11310540&url=%2Futvonalterv.jspv%3Fto%3D%26from%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26fromX%3D17.414611005499122%26fromY%3D46.57928886409497%26activeTab%3DrouteSearch&activeTab=routeSearch'" href="#">innen</a> <strong>/</strong> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&routePlanCount=11310540&url=%2Futvonalterv.jspv%3Ffrom%3D%26to%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26toX%3D17.414611005499122%26toY%3D46.57928886409497%26activeTab%3DrouteSearch'" href="#">ide</a> </li>
</ul>
<div class="contacts" sizcache="1" sizset="38">
<div class="phoneHolder clearfix" sizcache="1" sizset="38">
<div class="dt phone">
Telefonszám:
</div>
<div class="dd phoneValue tel" sizcache="1" sizset="38">
<span>(70) 326 2843</span> <a class="clickToCall" onclick="javascript:openECS('11310540');" href="#">Hívja ingyen!</a>
</div>
</div>
<div class="webLinkHolder" sizcache="1" sizset="39">
<div class="dt webLink clearfix">
Weboldal:
</div>
<div class="dd webLinkValue" sizcache="1" sizset="39">
<a href="http://www.ezerszirom.hu" rel="blank,nofollow">http://www.ezerszirom.hu</a>
</div>
</div>
</div>
</div>
</div>
And my code:
results = htmlDoc.DocumentNode.SelectNodes("//div[contains(@class, 'vcard')]");
if (results != null)
{
foreach (var node in results)
{
Response.Write(node.SelectSingleNode("//p[@class='description']").InnerText + "<br>");
}
}
the problem ? since i am iterating over the results, each node should have its own description, but i am getting the InnerText of the first node. any idea why?
In XPath, //
stands for "recursively search from the root". Therefore the query doesn't search from the node you have, but gets the first from the document.
Try this:
node.SelectSingleNode("descendant::p[@class='description']")
精彩评论