Using XPath, choose a single node from a node collection using HtmlAgilityPack.

html-agility-pack selectsinglenode 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>
        <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...</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>
        <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&amp;routePlanCount=11310540&amp;url=%2Futvonalterv.jspv%3Fto%3D%26from%3D8700+Marcali%2C+Pet%C5%91fi+S.+utca+11+.%26fromX%3D17.414611005499122%26fromY%3D46.57928886409497%26activeTab%3DrouteSearch&amp;activeTab=routeSearch'" href="#">innen</a> <strong>/</strong> <span>-</span> <a onclick="window.location = '/redirect.jspv?method=Redirect&amp;routePlanCount=11310540&amp;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>
            <div class="contacts" sizcache="1" sizset="38">
                <div class="phoneHolder clearfix" sizcache="1" sizset="38">
                    <div class="dt phone">
                    <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 class="webLinkHolder" sizcache="1" sizset="39">
                    <div class="dt webLink clearfix">
                    <div class="dd webLinkValue" sizcache="1" sizset="39">
                        <a href="" rel="blank,nofollow"></a>

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?

7/23/2011 10:20:41 AM

Accepted Answer

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:

7/23/2011 10:38:32 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow