Html Agility Pack DescendantsAndSelf
public IEnumerable < HtmlNode > DescendantsAndSelf()
Gets a collection of all descendant nodes of this element, in document order. The DescendantsAndSelf method is a member of HtmlAgilityPack.HtmlNode
Returns:
Returns a collection of all descendant nodes of this element, in document order.
Example
The following example displays the name of all the descendants and the node itself.
var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var node = htmlDoc.DocumentNode.SelectSingleNode("//body"); foreach (var nNode in node.DescendantsAndSelf()) { if (nNode.NodeType == HtmlNodeType.Element) { Console.WriteLine(nNode.Name); } }
Click here to run this example.
public IEnumerable < HtmlNode > DescendantsAndSelf(string name)
Get all descendant nodes with a matching name and the node itself. The DescendantsAndSelf method is a member of HtmlAgilityPack.HtmlNode
Parameters:
name: The name of the descendant node.
Returns:
Returns a collection of all descendants with a matching name and the node itself.
Example
The following example displays the name of all the descendant nodes with a matching name and the node itself.
var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var node = htmlDoc.DocumentNode.SelectSingleNode("//body"); foreach (var nNode in node.DescendantsAndSelf("h2")) { if (nNode.NodeType == HtmlNodeType.Element) { Console.WriteLine(nNode.OuterHtml); } }
Click here to run this example.