Z.Example.Clone

CSharp · Console

/Program.cs

// @nuget: HtmlAgilityPack

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

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);

        var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
		
		DisplayChildNodes(htmlBody);

        HtmlNode newHtmlBody = htmlBody.Clone();
		
		Console.WriteLine("\nDuplicate Node Name: " + newHtmlBody.Name);
	
        Console.WriteLine("\n****Display children of the duplicate node****\n");

        DisplayChildNodes(newHtmlBody);
	}
	
	static void DisplayChildNodes(HtmlNode nodeElement)
    {
        HtmlNodeCollection childNodes = nodeElement.ChildNodes;

        foreach (var node in childNodes)
        {
            if (node.NodeType == HtmlNodeType.Element)
			{
				Console.WriteLine(node.InnerText);
			}
        }
    }
}
;