Z.Example.Ancestors

CSharp · Console

/Program.cs

// @nuget: HtmlAgilityPack

using System;
using System.Xml;
using HtmlAgilityPack;
					
public class Program
{
	public static void Main()
	{
		var html =
        @"<!DOCTYPE html>
<html>
<body>
	<h1>This is <b>bold</b> heading</h1>
	<p>This is <u>underlined</u> paragraph</p>
	<h2>This is <i>italic</i> heading</h2>
</body>
</html> ";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
		
		var node = htmlDoc.DocumentNode.SelectSingleNode("//b");

        foreach (var nNode in node.Ancestors())
        {
            if (nNode.NodeType == HtmlNodeType.Element)
            {
                Console.WriteLine(nNode.Name);
            }

        }		
	}
}
;