Load HTML with Aspose.HTML for .NET

Aspose.HTML for .NET loads HTML into an HTMLDocument , the in-memory document tree used for DOM queries, content changes, data extraction, saving, and conversion. The available HTMLDocument constructors accept a local path, remote URL, markup string, or stream, so an application can choose the source that matches its workflow.


Load an HTML File in C#

Pass a local file path to the HTMLDocument constructor. Once loading is complete, the document exposes the parsed DOM. The following example opens input.html and reads the complete markup of the document element through OuterHTML .

C# Code to Load an HTML Document from a File

using System;
using Aspose.Html;

using HTMLDocument document = new HTMLDocument("input.html");

string html = document.DocumentElement.OuterHTML;
Console.WriteLine(html);

Choose the HTML Source and Constructor

  • Local file. Pass the file path when the HTML already exists on disk and relative resources should resolve from that document location.
  • Remote URL. Pass an HTTP or HTTPS address when the application must load a webpage and its permitted external resources.
  • Markup string. Pass the HTML code together with a base URL when markup is already available in memory.
  • Stream. Pass a readable stream and base URL when HTML comes from an upload, response body, database, or another in-memory source.

Load HTML from a URL in C#

The single-address constructor can open a remote webpage. URL loading also processes external resources that are accessible and allowed by the environment configuration. After loading, use DOM properties and methods to inspect or modify the document.

C# Code to Load an HTML Document from a URL

using System;
using Aspose.Html;

using HTMLDocument document =
    new HTMLDocument("https://docs.aspose.com/html/net/creating-a-document/");

string html = document.DocumentElement.OuterHTML;
Console.WriteLine(html);

Load HTML from a String in C#

Use the two-argument constructor when the application already has HTML markup in a string. The second argument is the base URL used to resolve relative images, stylesheets, scripts, fonts, and links. This self-contained example uses "." for the current directory; provide the actual directory or web address when the markup contains relative references.

C# Code to Create an HTML Document from a String

using System;
using Aspose.Html;

string markup = "<p>HTML loaded from a string</p>";

// Use the current directory as the base URL
string baseUrl = ".";

using HTMLDocument document = new HTMLDocument(markup, baseUrl);

string html = document.DocumentElement.OuterHTML;
Console.WriteLine(html);

Load HTML from a Stream in C#

The stream constructor reads from the stream’s current position. When the application has just written the HTML, flush the writer and reset the position before creating the document. Keep the stream open until HTMLDocument has finished reading it. The example uses "." as a neutral current-directory base because its markup has no relative resources.

C# Code to Load an HTML Document from a Stream

using System;
using System.IO;
using System.Text;
using Aspose.Html;

using MemoryStream stream = new MemoryStream();

// Keep the stream open after disposing the writer
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
{
    writer.Write("<p>HTML loaded from a stream</p>");
    writer.Flush();
}

// Reset the stream before HTMLDocument reads it
stream.Position = 0;

using HTMLDocument document =
    new HTMLDocument(stream, ".");

string html = document.DocumentElement.OuterHTML;
Console.WriteLine(html);

HTML Loading Behavior and Considerations

  • Constructors parse HTML into a DOM. The resulting tree follows HTML parsing rules. Serializing it later can normalize structure or markup, so do not expect a byte-for-byte copy of the source.
  • A base URL controls relative resources. String and stream inputs do not have an inherent document location. Supply the location that should resolve relative CSS, images, scripts, fonts, and links.
  • Document constructors load synchronously. They wait for document loading and permitted external resources. Use Navigate() and document events when the application requires asynchronous navigation.
  • Remote loading depends on the environment. Invalid addresses, redirects, authentication, network failures, and security restrictions can prevent a webpage or linked resource from loading.
  • Dispose loaded documents. HTMLDocument holds document and resource state. A using declaration or using block releases that state when processing is complete.

HTML Loading Documentation and API Reference

Read Create or Load HTML Documents for constructor examples, base URL guidance, asynchronous navigation, and common loading issues. After loading a document, use the HTML DOM guide to navigate its nodes or the Edit HTML Document guide to change content and attributes. For a new document that does not have an existing source, see Create HTML in C# .


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.


HTML Loading FAQ

1. How do I load an HTML document in C#?

Pass a local path or URL to an HTMLDocument constructor. For markup held in a string or stream, pass the content together with a base URL. The constructor parses the source and exposes the resulting HTML DOM.

2. Does loading HTML preserve the source markup exactly?

Do not rely on byte-level identity. Aspose.HTML parses the source into a DOM, and later serialization can normalize implied elements, character encoding, whitespace, or markup structure while preserving the document model.

3. Can Aspose.HTML load a webpage asynchronously?

Yes. Constructors perform synchronous loading. Follow Load HTML Asynchronously to create the document, subscribe to loading events such as OnReadyStateChange or OnLoad, call Navigate(), and wait with an application-defined timeout.

4. Why can a remote HTML document fail to load?

Common causes include an invalid or unreachable URL, redirects, authentication requirements, network restrictions, blocked external resources, or environment security settings. Validate the address and configure only the network access the application requires.

5. How can an application restrict external resources while loading HTML?

Create the document with a Configuration that applies the required security and resource-loading settings. Limit remote access to trusted locations, especially when the HTML or URL comes from an untrusted user.

Related HTML Document Workflows

Continue with document creation, editing, selection, or structured queries.