To create a new HTML document from scratch using the Html Agility Pack in C#, you need to instantiate a new HtmlDocument
object and then use its methods and properties to build the HTML content.
Here's a step-by-step guide to create a simple HTML document with Html Agility Pack:
- Install the Html Agility Pack package by running the following command in the Package Manager Console in Visual Studio:
Install-Package HtmlAgilityPack
- In your C# code, import the Html Agility Pack namespace:
using HtmlAgilityPack;
- Create a new instance of
HtmlDocument
:
var htmlDoc = new HtmlDocument();
- Create the structure of your HTML document. For example, let's create a document with a
DOCTYPE
declaration, anhtml
element,head
with atitle
, and abody
with anh1
element:
// Create the DOCTYPE declaration
HtmlDocumentType doctype = htmlDoc.CreateDocumentType("html", null, null, null);
htmlDoc.DocumentNode.AppendChild(doctype);
// Create the root <html> element
HtmlNode htmlNode = htmlDoc.CreateElement("html");
htmlDoc.DocumentNode.AppendChild(htmlNode);
// Create the <head> element with a <title>
HtmlNode headNode = htmlDoc.CreateElement("head");
htmlNode.AppendChild(headNode);
HtmlNode titleNode = htmlDoc.CreateElement("title");
titleNode.AppendChild(htmlDoc.CreateTextNode("My New HTML Document"));
headNode.AppendChild(titleNode);
// Create the <body> element with an <h1>
HtmlNode bodyNode = htmlDoc.CreateElement("body");
htmlNode.AppendChild(bodyNode);
HtmlNode h1Node = htmlDoc.CreateElement("h1");
h1Node.AppendChild(htmlDoc.CreateTextNode("Hello, World!"));
bodyNode.AppendChild(h1Node);
- Save the document to a file or retrieve its string representation:
To save to a file:
htmlDoc.Save("newHtmlDocument.html");
To get the HTML as a string:
string htmlString = htmlDoc.DocumentNode.OuterHtml;
Here is the complete code snippet:
using System;
using HtmlAgilityPack;
namespace HtmlAgilityPackExample
{
class Program
{
static void Main(string[] args)
{
var htmlDoc = new HtmlDocument();
// Create the DOCTYPE declaration
HtmlDocumentType doctype = htmlDoc.CreateDocumentType("html", null, null, null);
htmlDoc.DocumentNode.AppendChild(doctype);
// Create the root <html> element
HtmlNode htmlNode = htmlDoc.CreateElement("html");
htmlDoc.DocumentNode.AppendChild(htmlNode);
// Create the <head> element with a <title>
HtmlNode headNode = htmlDoc.CreateElement("head");
htmlNode.AppendChild(headNode);
HtmlNode titleNode = htmlDoc.CreateElement("title");
titleNode.AppendChild(htmlDoc.CreateTextNode("My New HTML Document"));
headNode.AppendChild(titleNode);
// Create the <body> element with an <h1>
HtmlNode bodyNode = htmlDoc.CreateElement("body");
htmlNode.AppendChild(bodyNode);
HtmlNode h1Node = htmlDoc.CreateElement("h1");
h1Node.AppendChild(htmlDoc.CreateTextNode("Hello, World!"));
bodyNode.AppendChild(h1Node);
// Save the document to a file
htmlDoc.Save("newHtmlDocument.html");
// Or get the HTML as a string
string htmlString = htmlDoc.DocumentNode.OuterHtml;
Console.WriteLine(htmlString);
}
}
}
This code will generate an HTML document with the following content:
<!DOCTYPE html>
<html>
<head>
<title>My New HTML Document</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
Remember to handle exceptions and edge cases as required for your specific use case.