How to Edit MHTML in C#
MHTML files represent a web page archive format that can be created by a number of different applications. The format is known as archive format because it saves the web HTML code and associated resources in a single file.
We will edit MHTML files using
Aspose.HTML for .NET API
which is a feature-rich, powerful and easy to use API for C# platform. The
Aspose.Html.Dom
(Document Object Model) 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.
Note: MHTML document editing goes through the stage of HTML document editing. You can load an HTML document, edit it and save it in MHTML format.
Code to Edit MHTML File using C#
Using the C# library, you can programmatically edit MHTML documents – change the document structure, style, and content. Let’s look at how to style <p>
paragraph element in an MHTML file. We add the style
attribute to the first paragraph element in the document.
C# code to edit MHTML files
// Prepare path to source HTML file
string documentPath = Path.Combine(DataDir, "file.html");
// Prepare path for edited MHTML file saving
string savePath = Path.Combine(OutputDir, "file-edited-style.mhtml");
// Initialize an HTML document from the file
using var document = new HTMLDocument(documentPath);
// Create a CSS Selector that extracts the first paragraph element in the document
var element = document.QuerySelector("p");
// Print content of the first paragraph
Output.WriteLine(element.InnerHTML);
// output: Aspose.HTML for .NET is a cross-platform class library that enables your applications to perform a wide range of HTML manipulation tasks.
// Set style attribute with properties for the selected element
element.SetAttribute("style", "color:rgb(50,150,200); background-color:#e1f0fe;");
// Create MHTML save options object
var options = new MHTMLSaveOptions();
// Save the HTML document as an MHTML file
document.Save(savePath, options);
Steps to style paragraph element in MHTML Document using C#
Consider simple steps to edit an existing document. In the document we will style the first paragraph:
- Load an HTML document using one of HTMLDocument() constructors. You can load HTML from a file, HTML code, stream, or URL.
- Create a CSS Selector that extracts the first element in the document which match selector. Use the
QuerySelector(“p”)
method of the Document class that returns the first
<p>
element in document. - Set style attribute with properties for the selected element. Use SetAttribute() method to add the style attribute with its property & value pairs.
- Create an MHTML save options object using the MHTMLSaveOptions() constructor.
- Save the edited HTML file as an MHTML document using the Save() method.
How to Remove Element from MHTML File in C#
The following C# code example shows how to remove the last paragraph in an MHTML document:
C# code to edit MHTML files
// Prepare path to source HTML file
string documentPath = Path.Combine(DataDir, "file.html");
// Prepare path for edited MHTML file saving
string savePath = Path.Combine(OutputDir, "file-remove-p.mhtml");
// Initialize an HTML document from the file
using var document = new HTMLDocument(documentPath);
// Find the last document paragraph element
var p = document.GetElementsByTagName("p").Last();
// Remove paragraph element
p.Remove();
// Create MHTML save options object
var options = new MHTMLSaveOptions();
// Save the HTML document as an MHTML file
document.Save(savePath, options);
Documentation
To learn more about Aspose.HTML API, please visit our
documentation
guide and
API Reference.
You can download the examples and showcase projects from the
GitHub
repository. They are open source and can be freely used in your own applications.
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.
The articles in the How-To Articles chapter answer popular questions about how to work with HTML files. In addition, the articles contain C# examples that provide the necessary information about using the Aspose.HTML class library to solve specific tasks, such as how to change the text style in a paragraph, how to effectively apply selectors to select the elements you want to edit, etc.
FAQ
The Aspose.HTML for .NET API is a standalone MHTML 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 MHTML documents.
Please visit our documentation to learn more about using the Aspose.HTML for .NET API to edit MHTML. If you have questions about functionality, found issues, or need a new feature, please start a discussion in our free support forum .
You can edit MHTML documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.
We support a few file formats that you can edit in C#, including HTML, MHTML and Markdown.
Get Started with Aspose.HTML for .NET API
You can use several ways to install the Aspose.HTML for .NET library on your system:
- Install a NuGet Package using the NuGet Package Manager GUI.
- Install a NuGet Package using the Package Manager Console. You may use the following command
PM> Install-Package Aspose.Html
. - 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.