Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To save an HTML document in C#, create or load an HTMLDocument, update the DOM if needed, and call HTMLDocument.Save(). Use HTMLSaveOptions when you need to control how related resources are saved, or HTMLSaveFormat when saving HTML as MHTML or Markdown.
After downloading an existing file or creating an HTML document from scratch, you can save changes using one of the HTMLDocument.Save() methods. Aspose.HTML for .NET provides overloads for saving a document to a file, URL, stream, or custom resource handler.
This article focuses on the common save workflows for HTML-like documents: saving an HTML file, saving HTML as MHTML or Markdown, and saving SVG documents. If your document has linked CSS, images, scripts, or you need custom output storage, see Save HTML with Resources.
The API provides two related but different output approaches:
The
HTMLDocument class represents an HTML document in memory. Once you create or load a document and modify it through the DOM, call one of the Save() overloads to write the current document state to output.
| API | Use it to control |
|---|---|
| HTMLDocument.Save() | Saves the current HTML document to a file, URL, stream, or resource handler depending on the overload. |
| SaveOptions | Provides base save settings and access to ResourceHandlingOptions for linked resources. |
| ResourceHandlingOptions | Controls how linked resources are handled. The Default behavior is Save, meaning resources are saved as files when resource saving applies. |
| HTMLSaveFormat | Selects an HTML save format such as HTML, MHTML, or Markdown when using format-based save overloads. |
| SVGDocument | Represents SVG documents and supports similar loading, editing, and saving workflows. |
The simplest save workflow is to create an HTML document, add content, and save it as a local .html file. This is useful when you generate HTML programmatically or modify an existing document and need to persist the result.
To save an HTML document to a file in C#:
The following C# example shows the easiest way to save an HTML file:
1// Save HTML to a file using C#
2
3// Prepare an output path for a document saving
4string documentPath = Path.Combine(OutputDir, "save-to-file.html");
5
6// Initialize an empty HTML document
7using (HTMLDocument document = new HTMLDocument())
8{
9 // Create a text element and add it to the document
10 Text text = document.CreateTextNode("Hello, World!");
11 document.Body.AppendChild(text);
12
13 // Save the HTML document to the file on a disk
14 document.Save(documentPath);
15}In the example above, the HTMLDocument() constructor initializes an empty HTML document. The CreateTextNode(data) method creates a text node, and Save(path) writes the document to the file path you provide.
For documents that reference CSS, images, scripts, or other files, saving only the main HTML file is often not enough. Use Save HTML with Resources when you need to save linked files together with the document.
The HTMLSaveFormat enumeration specifies the format in which an HTML document is saved. It can be used when you need an HTML-like output format rather than rendered PDF, XPS, DOCX, or image output.
In some cases, you need to save a web page as a single file. MHTML is a web page archive format that stores the HTML and related resources inside one file. The following example shows how to use Save(path, saveFormat) for HTML to MHTML saving.
To save HTML as MHTML in C#:
HTMLDocument.document.Save(path, HTMLSaveFormat.MHTML)..mht file as a single-file web page archive. 1// Save HTML as MHTML using C#
2
3// Prepare an output path for a document saving
4string savePath = Path.Combine(OutputDir, "save-to-mhtml.mht");
5
6// Prepare a simple HTML file with a linked document
7File.WriteAllText("save-to-mhtml.html", "<p>Hello, World!</p>" +
8 "<a href='linked-file.html'>linked file</a>");
9
10// Prepare a simple linked HTML file
11File.WriteAllText("linked-file.html", "<p>Hello, linked file!</p>");
12
13// Load the "save-to-mhtml.html" into memory
14using (HTMLDocument document = new HTMLDocument("save-to-mhtml.html"))
15{
16 // Save the document to MHTML format
17 document.Save(savePath, HTMLSaveFormat.MHTML);
18}The saved save-to-mhtml.mht file stores the HTML content from save-to-mhtml.html and the linked linked-file.html file.
Markdown is a markup language with plain-text syntax. You can use HTMLSaveFormat for HTML to Markdown saving when you need editable Markdown output from an HTML document.
To save HTML as Markdown in C#:
HTMLDocument.document.Save(path, HTMLSaveFormat.Markdown)..md file in Markdown-based documentation or content workflows. 1// Save HTML as Markdown using C#
2
3// Prepare an output path for a document saving
4string documentPath = Path.Combine(OutputDir, "save-html-to-markdown.md");
5
6// Prepare HTML code
7string html_code = "<H2>Hello, World!</H2>";
8
9// Initialize a document from a string variable
10using (HTMLDocument document = new HTMLDocument(html_code, "."))
11{
12 // Save the document as a Markdown file
13 document.Save(documentPath, HTMLSaveFormat.Markdown);
14}For converter-based HTML to Markdown workflows and conversion options, see Convert HTML to Markdown.
Usually, SVG appears as part of an HTML file and is used to represent vector graphics such as images, icons, charts, or diagrams. However, SVG can also be loaded and manipulated as a standalone document.
Since SVGDocument and HTMLDocument are based on the same WHATWG DOM standard, loading, reading, editing, converting, and saving workflows are similar for both document types.
To save an SVG document in C#:
Save() method with the target file path..svg file as a standalone vector document. 1// Create and save SVG image using C#
2
3// Prepare an output path for a document saving
4string documentPath = Path.Combine(OutputDir, "create-and-save-svg.svg");
5
6// Prepare SVG code
7string code = @"
8 <svg xmlns='http://www.w3.org/2000/svg' height='200' width='300'>
9 <g fill='none' stroke-width= '10' stroke-dasharray='30 10'>
10 <path stroke='red' d='M 25 40 l 215 0' />
11 <path stroke='black' d='M 35 80 l 215 0' />
12 <path stroke='blue' d='M 45 120 l 215 0' />
13 </g>
14 </svg>";
15
16// Initialize an SVG instance from the content string
17using (SVGDocument document = new SVGDocument(code, "."))
18{
19 // Save the SVG file to a disk
20 document.Save(documentPath);
21}For more information about SVG basics, drawing, and API usage for processing and rendering SVG documents, see the Aspose.SVG for .NET Documentation.
| Problem | Cause | Solution |
|---|---|---|
| Saved HTML misses images or CSS | Only the main HTML file was saved, or linked resources were not handled as required. | Use SaveOptions and resource handling workflows described in
Save HTML with Resources. |
MHTML output is expected but an .html file is created | The save overload without HTMLSaveFormat.MHTML was used. | Use Save(path, HTMLSaveFormat.MHTML) when a single-file web archive is required. |
| Markdown output differs from rendered page appearance | Markdown stores document structure and text, not a rendered visual representation. | Use Markdown output for editable content workflows; use conversion/rendering workflows for PDF, images, or fixed-layout output. |
| SVG changes are not saved | The SVGDocument was modified in memory but Save() was not called with the target path. | Save the SVG document after DOM changes are complete. |
Create or load an HTMLDocument, modify it if needed, and call document.Save(path) to write it to a local file.
Saving keeps an HTML-like document format and uses SaveOptions. Rendering creates a visual output such as PDF, XPS, DOCX, or images and uses rendering options.
Yes. Use HTMLSaveFormat.MHTML to save HTML as an MHTML web archive when the workflow requires a single file.
Yes. Use HTMLSaveFormat.Markdown when you need Markdown output from an HTML document.
The complete C# examples and data files are available in the Aspose.HTML for .NET GitHub repository.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.