Muhammad Ehsan Mirzaei
Muhammad Ehsan Mirzaei

Reputation: 129

select element with agility pack

I need to read a html file with agility pack that is contain

<span class=\"price\">
<span  itemprop=\"offers\" itemscope itemtype=\"http://schema.org/Offer\"  class='current'>  
<span  itemprop="price">8,160,000 ریال  </span> </span>
<span class=\"price-last-update\" original-title=\"تاریخ به روز رسانی\">1396/03/23  </span> </span>

above code is part of the html that repeat for times and I want to select this part

<span  itemprop="price">8,160,000 ریال

with this code:

string price = node.SelectSingleNode("/span/span/span[@class='price'").InnerText;

but it's don't work, thanks for your help

Upvotes: 0

Views: 235

Answers (1)

dovid
dovid

Reputation: 6462

var txt = @"
<span class='price'>
<span  itemprop='offers' itemscope itemtype='http://schema.org/Offer'  class='current'>
        <span  itemprop='price'>8,160,000 ریال  </span>
    </span>
    <span class='price-last-update' original-title='تاریخ به روز رسانی'>1396/03/23  </span>
</span>
";

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(txt);

Console.WriteLine(doc.DocumentNode.SelectSingleNode("/span[@class='price']").InnerText);
//  8,160,000 ریال   1396/03/23
Console.WriteLine(doc.DocumentNode.SelectSingleNode("//span[@class='price']/span").InnerText);
//  8,160,000 ریال

Upvotes: 0

Related Questions