user2244804
user2244804

Reputation:

html agility pack issue in C#

I want to get link,title and price from this html(this is one result of ten results)

 <div class="listing-item">

        <div class="block item-title">
            <h3 id="title">           
                <span style="direction: ltr" class="title">
                    <a xtcltype="S" xtclib="listing_list_1_title_link" href="http://dubai.dubizzle.com/motors/used-cars/ford/explorer/2013/7/1/ford-explorer-2012-new-model-expat-leaving-2/?back=ZHViYWkuZHViaXp6bGUuY29tL21vdG9ycy91c2VkLWNhcnMv&amp;pos=1">FORD EXPLORER - 2012 - NEW MODEL - EXPAT LEAV...</a>
                </span>
            </h3>
    <div class="price">
                    AED 118,000
                    <br>
            </div>
        </div>

</div>

Here is my code

 var allCarResults = rootNode.SelectNodes("//div[normalize-space(@class)='listing-item']");
        foreach (var carResult in allCarResults)
        {
            var dataNode = carResult.SelectSingleNode(".//div[@class='block item-title']");
            var carNameNode = dataNode.SelectSingleNode(".//h3/a");
            string carName = carNameNode.InnerText.Trim();
        }

This give me object reference issue to get carName.What mistake i am doing here?

Upvotes: 0

Views: 109

Answers (1)

Hans Kesting
Hans Kesting

Reputation: 39339

dataNode.SelectSingleNode(".//h3/a"); tries to select a <a> node directly under the <h3> that is somewhere under that dataNode.

However, in your case there is a <span> inbetween. So use dataNode.SelectSingleNode(".//h3//a"); (note the // between h3 and a) to get an <a> node somewhere below a <h3>.

Upvotes: 2

Related Questions