Save HTML Document – C# Examples

After downloading an existing file or creating an HTML document from scratch, you can save the changes using one of the HTMLDocument.Save() methods. There are overloaded methods to save a document to a file or a URL. The API provides Aspose.Html.Saving Namespace with the SaveOptions and ResourceHandlingOptions classes that allow you to set options for saving operations.

Please note that we have two different concepts for creating the output files. The first conception is based on producing the HTML like files as output; the SaveOptions as a base class for this approach helps to handle the saving process of related resources such as scripts, styles, images, etc. The second concept could be used to creating a visual representation of HTML as a result. The base class for this conception is RenderingOptions; it has specialized methods to specify the page size, page-margins, resolution, user-styles, etc. This article only describes how to use SaveOptions. To read more about the rendering mechanism, please follow the Renderers and Rendering Options articles.

SaveOptions & ResourceHandlingOptions

The SaveOptions is a base class that allows you to specify additional options for saving operations and helps to manage the linked resources. The ResourceHandlingOptions property of the SaveOptions class gets a ResourceHandlingOptions object which is used for configuration of resources handling. The ResourceHandlingOptions class represents resource handling options and the list of available ones are demonstrated in the following table:

OptionDescription
UrlRestrictionApplies restrictions to the host or folders where resources are located.
MaxHandlingDepthIf you need to save not the only specified HTML document, but also the linked HTML pages, this option gives you the ability to control the depth of the linked pages that should be saved.
JavaScriptThis option specifies how do we need to treat the JavaScript files: it could be saved as a separated linked file, embed into HTML file or even be ignored.
DefaultThis options specifies behavior for other than JavaScript files.

You can download the complete examples and data files from GitHub.

Save HTML

Once you have finished your changes as it is described here, you may want to save the document. You can do it using one of the Save() methods of the HTMLDocument class. The following example is the easiest way to save an HTML file:

 1using System.IO;
 2using Aspose.Html;
 3...
 4    // Prepare an output path for a document saving
 5	string documentPath = Path.Combine(OutputDir, "save-to-file.html");
 6
 7    // Initialize an empty HTML document
 8    using (var document = new HTMLDocument())
 9    {
10        // Create a text node and add it to the document
11        var text = document.CreateTextNode("Hello World!");
12        document.Body.AppendChild(text);
13
14        // Save the HTML document to a local file
15        document.Save(documentPath);
16    }

In the example above, we use the HTMLDocument() constructor for initializing an empty HTML document. The CreateTextNode(data) method of the HTMLDocument class creates a text node given the specified string. The Save(path) method saves the document to a local file specified by path.

The sample above is quite simple. However, in real-life applications, you often need additional control over the saving process. The next few sections describe how to use resource handling options or save you document to the different formats.

Save HTML to File

The following code snippet shows how to use ResourceHandlingOptions property of the SaveOptions class to manage linked to your document files.

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare an output path for a document
 6    string documentPath = Path.Combine(OutputDir, "save-with-linked-file.html");
 7
 8    // Prepare a simple HTML file with a linked document
 9    File.WriteAllText(documentPath, "<p>Hello World!</p>" +
10                                    "<a href='linked.html'>linked file</a>");
11    // Prepare a simple linked HTML file
12    File.WriteAllText("linked.html", "<p>Hello linked file!</p>");
13
14    // Load the "save-with-linked-file.html" into memory
15    using (var document = new HTMLDocument(documentPath))
16    {
17        // Create a save options instance
18        var options = new HTMLSaveOptions();
19
20        // The following line with value '0' cuts off all other linked HTML-files while saving this instance
21        // If you remove this line or change value to the '1', the 'linked.html' file will be saved as well to the output folder
22        options.ResourceHandlingOptions.MaxHandlingDepth = 1;
23
24        // Save the document with the save options
25        document.Save(Path.Combine(OutputDir, "save-with-linked-file_out.html"), options);
26    }

Save HTML to MHTML

In some cases, you need to save your web page as a single file. MHTML document could be handy and helpful for this purpose since it is a web-page archive and it stores everything inside itself. The HTMLSaveFormat Enumeration specifies the format in which document is saved, it can be HTML, MHTML, and MD formats. The example below shows how to use the Save(path,saveFormat) method for HTML to MHTML saving.

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare an output path for a document saving
 6    string documentPath = Path.Combine(OutputDir, "save-to-MTHML.mht");
 7
 8    // Prepare a simple HTML file with a linked document
 9    File.WriteAllText("document.html", "<p>Hello World!</p>" +
10                                       "<a href='linked-file.html'>linked file</a>");
11    // Prepare a simple linked HTML file
12    File.WriteAllText("linked-file.html", "<p>Hello linked file!</p>");
13
14    // Load the "document.html" into memory
15    using (var document = new HTMLDocument("document.html"))
16    {
17        // Save the document to MHTML format
18        document.Save(documentPath, HTMLSaveFormat.MHTML);
19
20    }

The saved “save-to-MTHML.mht” file stores HTML of the “document.html” and “linked-file.html” files.

Save HTML to Markdown

Markdown is a markup language with plain-text syntax. As well as for HTML to MHTML example, you can use the HTMLSaveFormat for HTML to MD saving. Please take a look at the following example:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Saving;
 4...
 5    // Prepare an output path for a document saving
 6    string documentPath = Path.Combine(OutputDir, "save-to-MD.md");
 7
 8    // Prepare HTML code
 9    var html_code = "<H2>Hello World!</H2>";
10
11    // Initialize a document from the string variable
12    using (var document = new HTMLDocument(html_code, "."))
13    {
14        // Save the document as a Markdown file
15        document.Save(documentPath, HTMLSaveFormat.Markdown);
16    }

For the more information how to use Markdown Converter, please visit the HTML to Markdown Conversion article.

Save SVG

Usually, you could see SVG as a part of an HTML file, it is used to represent the vector data on the page: images, icons, tables, etc. However, SVG also could be extracted from the web page and you can manipulate it in a similar way as the HTML document.

Since SVGDocument and HTMLDocument are based on the same WHATWG DOM standard, all operations such as loading, reading, editing, converting and saving are similar for both documents. So, all examples where you can see manipulation with the HTMLDocument are applicable for the SVGDocument as well.

To save your changes, please use follows:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Dom.Svg;
 4...
 5    // Prepare an output path for an SVG document saving
 6    string documentPath = Path.Combine(OutputDir, "save-to-SVG.svg");
 7
 8    // Prepare SVG code
 9    var code = @"
10        <svg xmlns='http://www.w3.org/2000/svg' height='200' width='300'>
11            <g fill='none' stroke-width= '10' stroke-dasharray='30 10'>
12                <path stroke='red' d='M 25 40 l 215 0' />
13                <path stroke='black' d='M 35 80 l 215 0' />
14                <path stroke='blue' d='M 45 120 l 215 0' />
15            </g>
16        </svg>";
17
18    // Initialize an SVG instance from the content string
19    using (var document = new SVGDocument(code, "."))
20    {
21        // Save the SVG file to a disk
22        document.Save(documentPath);
23    }

For more information about SVG Basics Drawing and and the API usage for the processing and rendering of SVG documents, see the Aspose.SVG for .NET Documentation.

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.