How to Edit HTML in C#

There are many ways you can edit HTML by using Aspose.HTML for .NET library. You can modify the document by inserting new nodes, removing, or editing the content of existing nodes. If you need to edit HTML documents programmatically in C#, the Aspose.Html.Dom (Document Object Model) namespace provides API that represents and interacts with any HTML documents. This namespace contains classes and methods to manipulate HTML documents on the fly. You can insert, remove, replace HTML nodes, extract CSS style information, get the content of elements, and navigate through HTML document by various data selectors.
Any document editing you want to perform involves loading an HTML document, editing and saving it in the supported format. It can be different scenarios, but it can be made with a few required steps:

  • Load an HTML document into a Document object using one of HTMLDocument() constructors. You can load HTML from a file, HTML code, stream, URL, or create from scratch.
  • Edit HTML DOM using classes and methods of Aspose.Html.Dom namespace.
  • Save HTML or convert HTML to the required file format.

How to Create Element and Add it to HTML File in C#

Using the C# library, you can programmatically edit HTML documents - change the document structure, style, and content. Let’s look at how to add a new HTML element such as <p> to an HTML file.

C# code to edit HTML files

    // Load HTML from a file
    var document = new HTMLDocument("document.html");
    var body = document.Body;

    // Create a new paragraph element
    var p = (HTMLParagraphElement)document.CreateElement("p");        
    // Create a text node
    var text = document.CreateTextNode("We add a new paragraph to an HTML document.");
    // Add the text to the paragraph
    // Add paragraph to the document body 
    // Save HTML file 

Steps to Create and Add Element to HTML Document in C#

Consider simple steps to edit existing HTML document. In the document will add a new text paragraph:

  1. Load an HTML document using one of HTMLDocument() constructors. You can load HTML from a file, HTML code, stream, or URL.
  2. Create a new paragraph element. Use the CreateElement() method of the Document class to create a paragraph element. In an HTML document, the Document.CreateElement() method creates the HTML element specified by tagName, in our case the tagName is p.
  3. Create text content for a new paragraph using CreateTextNode() method.
  4. Use AppendChild() method to add text content into the <p> element.
  5. Add the new paragraph to the document body using AppendChild() method.
  6. Save the edited HTML file with Save() method.

How to Remove Element from HTML File in C#

The following C# code example shows how to remove the first paragraph in an HTML document:

C# code to edit HTML files

    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "file.html");

    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);

    // Find the first document paragraph element 
    var p = document.GetElementsByTagName("p").First();

    // Remove paragraph element

    // Save the HTML document to a file
    document.Save(Path.Combine(OutputDir, "file-edited.html"));


To learn more about Aspose.HTML API, please visit our documentation guide. You can download the examples from the GitHub repository. They are open source and can be freely used in your own applications.

The Editing an 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.

Aspose.HTML for .NET is an advanced document processing API that allows you to create or open existing HTML documents from various sources in order to perform manipulation operations such as remove, replace and edit HTML nodes, save HTML documents, extract CSS from HTML, configure a document sandbox and more. For more information about classes and methods for editing and manipulating HTML documents, see the API Reference.


1. How can I edit HTML in C#?

The Aspose.HTML Library for .NET is a standalone HTML editing solution that does not depend on other software. Install our C# library, add the library reference to your C# project, and programmatically edit and manage HTML documents.

2. What file formats can I edit with Aspose.HTML C# library?

We support a few file formats that you can edit in C#, including HTML, MHTML and Markdown.

3. Can I edit HTML files on Linux, Mac OS, Android or iOS?

You can edit HTML documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.

Get Started with .NET HTML API

You can use several ways to install the Aspose.HTML library for .NET on your system:

  1. Install a NuGet Package using the NuGet Package Manager GUI.
  2. Install a NuGet Package using the Package Manager Console. You may use the following command PM> Install-Package Aspose.Html.
  3. Install Aspose.HTML for .NET through MSI.

This library supports parsing of HTML5, CSS3, SVG and HTML Canvas to construct a Document Object Model (DOM) based on the WHATWG DOM Standard. Aspose.HTML for .NET is written completely in C# and can be used to build any type of 32-bit or 64-bit .NET application including ASP.NET, WCF, WinForms & .NET Core. Before running the .NET conversion example code, make sure that you have OS like Microsoft Windows or a compatible with .NET Framework or .NET Standard, and the development environment like Microsoft Visual Studio. For more details about C# library installation and system requirements, please refer to Aspose.HTML Documentation.

Other Supported Editors

Edit files in other file formats: