Z.Example.Save

CSharp · Console

/Program.cs

// @nuget: HtmlAgilityPack

using System;
using System.Xml;
using System.IO;
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>
	<h2>This is new heading</h2>
</body>
</html> ";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
		
		StreamWriter sw = new StreamWriter("StreamWriter.html");

        htmlDoc.Save(sw);

        sw.Close();

        var htmlNewDoc = new HtmlDocument();

        htmlNewDoc.Load("StreamWriter.html");

        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");

        Console.WriteLine(node.OuterHtml);
	}
}
;