HtmlNode.SelectNodes issue

Sep 25, 2009 at 10:04 PM

when i use an Xpath of //element on a particular node, why am I getting all the nodes of the entire document of type //element rather than just the nodes within the node I call SelectNode on?  Is it possible to just query the node with SelectNodes for only childnodes with that element tag?

Oct 3, 2009 at 4:18 AM

Yes, it does grab all then nodes in the document. Internally it creates a navigator from the root of the document and then runs the xpath.

One of the new Methods HtmlNode.Descendants(string name) may be more to your liking.

var nodes = thenode.Descendants("input");
foreach(var node in nodes)
  //do something with the node

You can also go a step further and do

var nodes = thenode.Descendants("input")
                   .Where(x => x.Attributes.Contains("type") && 
                          x.Attributes["type"].Value.ToLower().Contains("hidden"));

foreach (var node in nodes)
    //do something with the node

You can also use CreateNavigator but be warned the types it returns are System.Xml types, not HAP types.