选择具有敏捷包的元素

时间:2017-06-18 06:44:08

标签: c# html-agility-pack

我需要阅读包含

的敏捷包的html文件

<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>

上面的代码是html的一部分,重复了一段时间 我想选择这部分

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

使用此代码:

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

但由于你的帮助,它不起作用

1 个答案:

答案 0 :(得分:0)

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 ریال