Html Agility Pack Foreach Reverse

184 Views Asked by At

I want to create a data collector. I am using HtmlNode foreach at below;

foreach (HtmlNode page in pages)
{
   string title = page.SelectSingleNode(".//h3").InnerText.ToString().Trim();
}

How can i collect all pages from end to home.

Data source;

<div><h3>Page title 1</h3></div>
<div><h3>Page title 2</h3></div>
<div><h3>Page title 3</h3></div>

My results; 1,2,3

I need; 3,2,1

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Don't know how you pages collection look like, but made a complete example below:

var doc = new HtmlDocument();
doc.LoadHtml("<html><body><div><h3>Page title 1</h3></div><div><h3>Page title 2</h3></div><div><h3>Page title 3</h3></div></body></html>");
var pages = doc.DocumentNode.SelectNodes("//div");

for (var i = pages.Count - 1; i >= 0; i--)
{
    string title = pages[i].SelectSingleNode(".//h3").InnerText.Trim();
}