How to Create HTML in C#

HTML (Hypertext Markup Language) is the standard language for creating web pages and constructing HTML documents is a fundamental skill for web developers and anyone working with web technologies. Aspose.HTML for .NET library provides a set of classes and methods for creating, editing, converting, and other manipulations with HTML files. So, let’s create a new HTML document!

First, make sure you have Aspose.HTML for .NET installed in your project. The installation process of this library is quite smooth. You can install it via NuGet Package Manager Console using the following command:


Install Aspose.HTML for .NET

Install-Package Aspose.HTML

HTML file can be created from scratch as an empty document with HTML structure. Here’s an example of how you might create a blank HTML document:


C# code to create an empty HTML file

using System.IO;
using Aspose.Html;
...

    // Initialize an empty HTML document
    using (var document = new HTMLDocument())
    {
        // Work with the document

        // Save the document to a file
        document.Save(Path.Combine(OutputDir, "create-empty-document.html"));
    }

  1. Create an HTML document from scratch using the HTMLDocument() constructor.
  2. Save the HTML file with the Save() method.

After running this example, you will end up with a new blank HTML document with the following basic structure:

  • <html> is a tag that marks the beginning and end of an HTML document. It is also called the document’s root element, and all other elements must be inside it.
  • <head> is one of the special elements because it contains metadata and its content is not displayed by the browser.
  • <body> is the document’s body, which should contain all the HTML page content. Or everything you see when you look at a page in a browser: headings, paragraphs, pictures, links, lists, etc.

The structure of an empty HTML document

<html>
	<head>
	</head>
	<body>
	</body>
</html>

How to Add Elements to HTML File

Once the document object is created, it can be filled with HTML elements. You can customize the document by adding more elements like headings, paragraphs, links, images, etc., using the appropriate classes provided by the Aspose.Html namespace. For example, this is how you can add a heading and paragraph:


C# code to edit HTML file

using System.IO;
using Aspose.Html;
...

    // Initialize an empty HTML Document
    using (var document = new HTMLDocument())
    {
        var body = document.Body;

        // Create a heading element (h1) and set its text
        var h1 = (HTMLHeadingElement)document.CreateElement("h1");
        var texth1 = document.CreateTextNode("Create HTML file");

        // Create a paragraph element (p) set its text
        var p = (HTMLParagraphElement)document.CreateElement("p");
        var text = document.CreateTextNode("Learn how to create HTML file");

        // Attach the text to the h1 and paragraph
        h1.AppendChild(texth1);
        p.AppendChild(text);

        // Attach h1 and paragraph to the document body
        body.AppendChild(h1);
        body.AppendChild(p);

        // Save the document to a disk
        document.Save(Path.Combine(OutputDir, "create-new-document.html"));
    }

Let’s look at the simple steps to edit an HTML document that illustrate the С# code above. An H1 heading and a text paragraph are added to the created document:

  1. To create an HTML document programmatically from scratch, use the HTMLDocument() constructor with no parameters.
  2. The Body property of the HTMLDocument class points to the document’s <body> element.
  3. Create heading <h1> and paragraph <p> elements. Use the CreateElement() method of the Document class to create <h1> and <p> elements.
  4. Use CreateTextNode() method to create text content for a new elements.
  5. Use AppendChild() method to add text content into the <h1> and <p> elements.
  6. Add the new elements to the document body using AppendChild() method.
  7. Save the edited HTML file with Save() method.

  • To learn more about Aspose.HTML API, please visit our documentation guide. From the Create HTML Document article, you will find information on how to load a document from a file, URL and stream or create it from scratch. The Edit HTML Document documentation article gives you basic information on how to read or edit the Document Object Model using Aspose.HTML for .NET API. You will explore how to create HTML elements and how to work with them – modify the document by inserting new nodes, removing, or editing the content of existing nodes.
  • You can see the Edit HTML page to find out Aspose.HTML for .NET API features to create, remove, or edit the content of an HTML document.