When HtmlNode instances are HtmlNode , multiple calls to the same node will return the same link. You can take advantage of this (although this is implementation detail, so be careful).
Basically, you will get all descendants that are elements prior to node. You select node to start with:
HtmlNode divNode = doc.DocumentNode.SelectSingleNode("div[@id='div1']");
node you want to go to:
// Note that in this case, working off the first node is not necessary, just // convenient for this example. HtmlNode brNode = divNode.SelectSingleNode("br");
And then use the TakeWhile extension method on the Enumerable class to take all the elements to the second element, for example:
This is a comparison in the TakeWhile method ( n => n != brNode ), which depends on link comparison (this is part of the implementation detail).
The last filter should only give you the element nodes, since you usually get SelectSingleNode calls; if you want to handle other types of node you can omit this.
Run through these nodes:
foreach (HtmlNode node in nodes) {
It produces:
Node: strong Node: font Node: font
source share