
HTML files are used to display data or information in web pages. Sometimes, you may need to create an HTML file within your projects. Following such scenarios, this article covers how to create an HTML file programmatically in C#.
- Create HTML Files in C# - API Installation
- How to Create an HTML File in C#
- Generate HTML from a URL in C#
Create HTML Files in C# - API Installation
Aspose.HTML for .NET API can be used to work with different supported file formats without needing to install any other application. You may download the DLL files from the New Releases page or use the following NuGet command:
PM> Install-Package Aspose.Html
How to Create an HTML File in C#
You can create an HTML file with simple method calls as explained in the steps below:
- Create an object of HTMLDocument class.
- Create a text element and append it to the document.
- Save the output HTML document.
- Save the HTML document.
The code sample below shows how to follow these steps and create an HTML file programmatically in C#:
// Prepare an output path for a document saving | |
string documentPath = "create-new-document.html"; | |
// Initialize an empty HTML document | |
using (var document = new Aspose.Html.HTMLDocument()) | |
{ | |
// Create a text element and add it to the document | |
var text = document.CreateTextNode("Hello World!"); | |
document.Body.AppendChild(text); | |
// Save the document to a disk | |
document.Save(documentPath); | |
} |
Generate HTML from a URL in C#
You can create an HTML file by following the steps below:
- Create an object of the Url class to specify the link.
- Initialize HTMLDocument class object while passing the Url as the parameter.
- Save the output HTML file.
The following sample code demonstrates how to create an HTML file from any online web page in C#:
// Set the Url link for online web page | |
Aspose.Html.Url url = new Aspose.Html.Url("https://blog.aspose.com/"); | |
// Initialize an object of HTMLDocument class | |
var document = new Aspose.Html.HTMLDocument(url); | |
// Save the document to a disk | |
document.Save(dataDir + "create-from-URL.html"); |
C# API to Generate HTML Files - Read More
You may visit the API documentation where different chapters cover other features of the API.
C# Create an HTML File with a Free License
You may request a free temporary license in order to evaluate the API without any evaluation limitations.
Conclusion
In this article, you have learned how to create an HTML web page programmatically in C#. It explains how to create an HTML file from scratch or while using the content from an online Url. Please feel free to write to us at forum in case you have any ambiguity.