I have a lots of Html block code with following style, I need values of
that I specified these number in a code.
Finally I want to put all these values in a XML file. So could you please help me regarding how I could get these values with HtmlAgilityPack?
Thanks in advance.
<div class="promotion">
<div class="logo">
<img src='http://www.example.com/D.jpg' **(1)**>
</div>
<div class="details">
<p class="date"> 2015/12/12 **(2)** </p>
<p>
<img src="http://www.example.com/DDD.jpg" **(3)** alt="" />
<h3> Some Details **(4)** </h3>
</p>
</div>
</div>
If you HTML is like you put in your question you can use XPath to retrieve your results in the following way ordered:
With a previous code like this, for example to test with your HTML :
var html = @"<div class='promotion'>
<div class='logo'>
<img src='http://www.example.com/D.jpg' **(1)**>
</div>
<div class='details'>
<p class='date'> 2015/12/12 **(2)** </p>
<p>
<img src='http://www.example.com/DDD.jpg' **(3)** alt='' />
<h3> Some Details **(4)** </h3>
</p>
</div>
</div>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
For the first image for example something like this :
var value = doc.DocumentNode.SelectSingleNode("//div[@class='logo']/img").Attributes["src"].Value;
For the second something like this :
var value = doc.DocumentNode.SelectSingleNode("//p[@class='date']").InnerText;
For the third :
var value = doc.DocumentNode.SelectSingleNode("//div[@class='details']/p[2]/img").Attributes["src"].Value;
And for the four :
var value = doc.DocumentNode.SelectSingleNode("//div[@class='details']/p[2]/h3").InnerText;
I hope this help you.