Navigating a Node Collection

Jan 18, 2011 at 6:49 PM

I am trying to parse up some HTML and am struggling with the documentation.

I'm trying to look at all of the <td> tags and have created an HtmlNodeCollection which contians all of the <td> elements, but I'm not sure the best way to navigate through the collection.

It doesn't have any built in methods for this so I thought I'd use an HtmlNodeNavigator, but there is no constructor that takes an HtmlNodeCollection.

I'm looking for a good way to be able to step through the HtmlNodeCollection, possibly even in a bi-directional fashion.

Jan 19, 2011 at 11:27 AM


foreach
(HtmlNode node in nodes) { }

or

for (int i=0;i<nodes.Count;i++)
{
   var node = nodes[i];
}

Jan 19, 2011 at 2:27 PM

The HtmlNodeNavigator is used internally to help implement XPATH.

It might be easier to just use LINQ

if you are trying to get all the <td> tags on the page

var nodes = doc.DocumentNode.Descendents("td");

// now you can foreach or for through them

foreach(var td in nodes)
{
 // do something with the td var
}

//you can also use LINQ to further weed out things like

var txtNodes = nodes.Where(td=>td.InnerText.Contains("$"));
//this will give you all the TD nodes that have text inside of them containing a dollar sign