HTMLDocument

HTMLDocument()

The HTMLDocument constructor creates a new HTML Document object that is a web page loaded in the browser and serving as an entry point into the page’s content.

public HTMLDocument()

Remarks

Note: The document is created with a default value for the base-url property that is equal to ‘about:blank’.

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

Once the document object is created, it can be filled later with HTML elements. The following code snippet shows the usage of the default HTMLDocument() constructor to create an empty HTML document and save it to a file.

using (var document = new HTMLDocument())
{
	// Work with the document here
	...	
	
	// Save the document to a file
	document.Save("document.html");
}

See Also


HTMLDocument(Configuration)

The HTMLDocument constructor creates a new HTML Document object that is a web page loaded in the browser and serving as an entry point into the page’s content.

public HTMLDocument(Configuration configuration)
ParameterTypeDescription
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Remarks

Note: The document is created with a default value for the base-url property that is equal to ‘about:blank’.

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

The following example demonstrates how to use the configuration object to disable scripts:

// Prepare HTML code and save it to a file
var code = "<span>Hello World!!</span> " +
		   "<script>document.write('Have a nice day!');</script>";

File.WriteAllText(Path.Combine(OutputDir, "sandboxing.html"), code);

// Create an instance of Configuration
using (var configuration = new Configuration())
{
	// Mark 'scripts' as an untrusted resource
	configuration.Security |= Sandbox.Scripts;

	// Initialize an HTML document with specified configuration
	using (var document = new HTMLDocument(Path.Combine(OutputDir, "sandboxing.html"), configuration))
	{
		// Convert HTML to PDF
		Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "sandboxing_out.pdf"));
	}
}

See Also


HTMLDocument(Url)

Loads the HTML document from a URL.

Note: In case if you pass a wrong URL that can’t be reached right at the moment, the library throws the DOMException with specialized code ‘NetworkError’ to inform you that the selected resource can not be found.

public HTMLDocument(Url url)
ParameterTypeDescription
urlUrlThe HTML document URL to open.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

Load a document from ‘https://docs.aspose.com/html/net/working-with-documents/creating-a-document/document.html' web page:

using (var document = new HTMLDocument("https://docs.aspose.com/html/net/working-with-documents/creating-a-document/document.html"))
{
	// Write the document content to the output stream
	Console.WriteLine(document.DocumentElement.OuterHTML);
}

See Also


HTMLDocument(Url, Configuration)

Loads the HTML document from a URL with specified environment configuration settings.

Note: In case if you pass a wrong URL that can’t be reached right at the moment, the library throws the DOMException with specialized code ‘NetworkError’ to inform you that the selected resource can not be found.

public HTMLDocument(Url url, Configuration configuration)
ParameterTypeDescription
urlUrlThe HTML document URL to open.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

The following example demonstrates how to use the configuration object to disable scripts:

// Prepare HTML code and save it to a file
var code = "<span>Hello World!!</span> " +
		   "<script>document.write('Have a nice day!');</script>";

File.WriteAllText(Path.Combine(OutputDir, "sandboxing.html"), code);

// Create an instance of Configuration
using (var configuration = new Configuration())
{
	// Mark 'scripts' as an untrusted resource
	configuration.Security |= Sandbox.Scripts;

	// Initialize an HTML document with specified configuration
	using (var document = new HTMLDocument(Path.Combine(OutputDir, "sandboxing.html"), configuration))
	{
		// Convert HTML to PDF
		Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "sandboxing_out.pdf"));
	}
}

See Also


HTMLDocument(string)

Loads the HTML document from an address.

Note: In case if you pass a wrong URL that can’t be reached right at the moment, the library throws the DOMException with specialized code ‘NetworkError’ to inform you that the selected resource can not be found.

public HTMLDocument(string address)
ParameterTypeDescription
addressStringThe HTML document address to open.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

Initialize an HTML document from an address.

using (var document = new HTMLDocument("./my-folder/document.html")))
{
	...
}

See Also


HTMLDocument(string, Configuration)

Loads the HTML document from an address with specified environment configuration settings.

Note: In case if you pass a wrong URL that can’t be reached right at the moment, the library throws the DOMException with specialized code ‘NetworkError’ to inform you that the selected resource can not be found.

public HTMLDocument(string address, Configuration configuration)
ParameterTypeDescription
addressStringThe HTML document address to open.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Create an instance of Configuration
using (var configuration = new Configuration())
{
	// Mark 'scripts' as an untrusted resource
	configuration.Security |= Sandbox.Scripts;
	
	using (var document = new HTMLDocument("./my-folder/document.html", configuration)))
	{
		...
	}
}

See Also


HTMLDocument(string, string)

Creates an HTML document from a String content with specified base-uri.

public HTMLDocument(string content, string baseUri)
ParameterTypeDescription
contentStringThe string content to load the document with.
baseUriStringThe base URI of the document.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Prepare HTML code
var html_code = "<p>Hello World!</p>";

// Initialize a document from the string variable
using (var document = new HTMLDocument(html_code, "."))
{
	...
}

See Also


HTMLDocument(string, string, Configuration)

Creates an HTML document from a String content with specified base-uri and environment configuration settings.

public HTMLDocument(string content, string baseUri, Configuration configuration)
ParameterTypeDescription
contentStringThe string content to load the document with.
baseUriStringThe base URI of the document.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Prepare HTML code
var html_code = "<p>Hello World!</p>";

// Initialize a document from the string variable
using (var document = new HTMLDocument(html_code, "."))
{
	...
}

See Also


HTMLDocument(string, Url)

Creates an HTML document from a String content with specified base-uri.

public HTMLDocument(string content, Url baseUri)
ParameterTypeDescription
contentStringThe string content to load the document with.
baseUriUrlThe base URI of the document.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Prepare HTML code
var html_code = "<p>Hello World!</p>";

// Initialize a document from the string variable
using (var document = new HTMLDocument(html_code, "."))
{
	...
}

See Also


HTMLDocument(string, Url, Configuration)

Creates an HTML document from a String content with specified base-uri and environment configuration settings.

public HTMLDocument(string content, Url baseUri, Configuration configuration)
ParameterTypeDescription
contentStringThe string content to load the document with.
baseUriUrlThe base URI of the document.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Prepare HTML code
var html_code = "<p>Hello World!</p>";

// Initialize a document from the string variable
using (var document = new HTMLDocument(html_code, "."))
{
	...
}

See Also


HTMLDocument(Stream, string)

Creates an HTML document from a Stream content with specified base-uri that is used to resolve the relative resources’ path.

public HTMLDocument(Stream content, string baseUri)
ParameterTypeDescription
contentStreamThe Stream content to load the document with.
baseUriStringThe base URI of the document.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Create a memory stream object
using (var mem = new MemoryStream())
using (var sw = new StreamWriter(mem))
{
	// Write the HTML code into memory object
	sw.Write("<p>Hello World! I love HTML!</p>");

	// It is important to set the position to the beginning since HTMLDocument starts the reading exactly from the current position within the stream
	sw.Flush();
	mem.Seek(0, SeekOrigin.Begin);

	// Initialize a document from the string variable
	using (var document = new HTMLDocument(mem, "."))
	{
		// Save the document to a disk
		document.Save("load-from-stream.html");
	}
}

See Also


HTMLDocument(Stream, string, Configuration)

Creates an HTML document from a Stream content with specified base-uri and environment configuration settings.

public HTMLDocument(Stream content, string baseUri, Configuration configuration)
ParameterTypeDescription
contentStreamThe Stream content to load the document with.
baseUriStringThe base URI of the document.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Create a memory stream object
using (var mem = new MemoryStream())
using (var sw = new StreamWriter(mem))
{
	// Write the HTML code into memory object
	sw.Write("<p>Hello World! I love HTML!</p>");

	// It is important to set the position to the beginning since HTMLDocument starts the reading exactly from the current position within the stream
	sw.Flush();
	mem.Seek(0, SeekOrigin.Begin);

	// Initialize a document from the string variable
	using (var document = new HTMLDocument(mem, "."))
	{
		// Save the document to a disk
		document.Save("load-from-stream.html");
	}
}

See Also


HTMLDocument(Stream, Url)

Creates an HTML document from a Stream content with specified base-uri that is used to resolve the relative resources’ path.

public HTMLDocument(Stream content, Url baseUri)
ParameterTypeDescription
contentStreamThe Stream content to load the document with.
baseUriUrlThe base URI of the document.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Create a memory stream object
using (var mem = new MemoryStream())
using (var sw = new StreamWriter(mem))
{
	// Write the HTML code into memory object
	sw.Write("<p>Hello World! I love HTML!</p>");

	// It is important to set the position to the beginning since HTMLDocument starts the reading exactly from the current position within the stream
	sw.Flush();
	mem.Seek(0, SeekOrigin.Begin);

	// Initialize a document from the string variable
	using (var document = new HTMLDocument(mem, "."))
	{
		// Save the document to a disk
		document.Save("load-from-stream.html");
	}
}

See Also


HTMLDocument(Stream, Url, Configuration)

Creates an HTML document from a Stream content with specified base-uri and environment configuration settings.

public HTMLDocument(Stream content, Url baseUri, Configuration configuration)
ParameterTypeDescription
contentStreamThe Stream content to load the document with.
baseUriUrlThe base URI of the document.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Exceptions

exceptioncondition
ArgumentNullExceptionThrows if the base-uri parament is null.

Remarks

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

Examples

// Create a memory stream object
using (var mem = new MemoryStream())
using (var sw = new StreamWriter(mem))
{
	// Write the HTML code into memory object
	sw.Write("<p>Hello World! I love HTML!</p>");

	// It is important to set the position to the beginning since HTMLDocument starts the reading exactly from the current position within the stream
	sw.Flush();
	mem.Seek(0, SeekOrigin.Begin);

	// Initialize a document from the string variable
	using (var document = new HTMLDocument(mem, "."))
	{
		// Save the document to a disk
		document.Save("load-from-stream.html");
	}
}

See Also


HTMLDocument(RequestMessage)

Creates an HTML document from the RequestMessage object.

public HTMLDocument(RequestMessage request)
ParameterTypeDescription
requestRequestMessageThe request message that contains a body with document content.

Remarks

By definition, a message handler is a class that receives a Web request and returns a Web response. In other words, a message handler is used to process a Web service request during input and/or to process the response during output.

Please, visit our docs site to see more scenarios on how to use this constructor.

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

See Also


HTMLDocument(RequestMessage, Configuration)

Creates an HTML document from the RequestMessage object.

public HTMLDocument(RequestMessage request, Configuration configuration)
ParameterTypeDescription
requestRequestMessageThe request message that contains a body with document content.
configurationConfigurationThe environment configuration such as scripts policy, custom user stylesheet, etc.

Remarks

By definition, a message handler is a class that receives a Web request and returns a Web response. In other words, a message handler is used to process a Web service request during input and/or to process the response during output.

Please, visit our docs site to see more scenarios on how to use this constructor.

Reference:

DOM Standard - defines a platform-neutral model for events, aborting activities, and node trees.DOM Standard (DOM) # htmldocument.GitHub - repository hosts the DOM Standard.

See Also