HtmlDocumentParser Class

Parses HTML documents and fragments according to the HTML Standard.

public sealed class HtmlDocumentParser

Constructors

NameDescription
HtmlDocumentParser(HtmlParserOptions?)Creates a parser with the given options, or with the default options when options is null.

HtmlDocumentParser(HtmlParserOptions?)

HtmlDocumentParser.cs

Creates a parser with the given options, or with the default options when options is null.

public HtmlDocumentParser(HtmlParserOptions? options = null)

Parameters

options HtmlParserOptions?

Methods

NameDescription
Parse(Stream)Parses an HTML document from a stream, determining the character encoding as specified.
Parse(ReadOnlySpan<byte>)Parses an HTML document from bytes, determining the character encoding as specified.
Parse(string)Parses an HTML document from a string (the encoding is irrelevant).
ParseAsync(Stream, CancellationToken)Parses an HTML document from a stream, determining the character encoding as specified, and waits until the parser has finished (see HtmlDocumentParser.ParseAsync).
ParseAsync(string, CancellationToken)Parses an HTML document from a string, and waits until the parser has finished: while the parser waits for a script, the tasks of the DomEventLoop of the browsing context run.
ParseDocument(string, HtmlParserOptions?)Parses an HTML document. The document has no browsing context: use BrowsingContext.Navigate to load a document in a browsing context.
ParseDocumentAsync(string, HtmlParserOptions?, CancellationToken)Parses an HTML document, and waits until the parser has finished: while the parser waits for a script, the tasks of the DomEventLoop of the browsing context run.
ParseFragment(string, Element)Runs the HTML fragment parsing algorithm (https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm) with context as the context element. The nodes are created in the context's node document.

Parse overloads

Parses an HTML document from a stream, determining the character encoding as specified.

public Document Parse(Stream stream)
Parameters
stream Stream
Returns

Document

Parse(ReadOnlySpan<byte>)

HtmlDocumentParser.cs

Parses an HTML document from bytes, determining the character encoding as specified.

public Document Parse(ReadOnlySpan<byte> bytes)
Parameters
bytes ReadOnlySpan<byte>
Returns

Document

Parses an HTML document from a string (the encoding is irrelevant).

public Document Parse(string html)
Parameters
html string
Returns

Document

ParseAsync overloads

ParseAsync(Stream, CancellationToken)

HtmlDocumentParser.cs

Parses an HTML document from a stream, determining the character encoding as specified, and waits until the parser has finished (see HtmlDocumentParser.ParseAsync).

net10.0

public Task<Document> ParseAsync(Stream stream, CancellationToken cancellationToken = default)

net11.0

public Task<Document> ParseAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
stream Stream
cancellationToken CancellationToken
Returns

Task<Document>

ParseAsync(string, CancellationToken)

HtmlDocumentParser.cs

Parses an HTML document from a string, and waits until the parser has finished: while the parser waits for a script, the tasks of the DomEventLoop of the browsing context run.

net10.0

public Task<Document> ParseAsync(string html, CancellationToken cancellationToken = default)

net11.0

public Task<Document> ParseAsync(string html, CancellationToken cancellationToken = default)
Parameters
html string
cancellationToken CancellationToken
Returns

Task<Document>

ParseDocument(string, HtmlParserOptions?)

HtmlDocumentParser.cs

Parses an HTML document. The document has no browsing context: use BrowsingContext.Navigate to load a document in a browsing context.

public static Document ParseDocument(string html, HtmlParserOptions? options = null)

Parameters

html string
options HtmlParserOptions?

Returns

Document

ParseDocumentAsync(string, HtmlParserOptions?, CancellationToken)

HtmlDocumentParser.cs

Parses an HTML document, and waits until the parser has finished: while the parser waits for a script, the tasks of the DomEventLoop of the browsing context run.

public static Task<Document> ParseDocumentAsync(string html, HtmlParserOptions? options = null, CancellationToken cancellationToken = default)

Parameters

html string
options HtmlParserOptions?
cancellationToken CancellationToken

Returns

Task<Document>

ParseFragment(string, Element)

HtmlDocumentParser.cs

Runs the HTML fragment parsing algorithm (https://html.spec.whatwg.org/multipage/parsing.html#html-fragment-parsing-algorithm) with context as the context element. The nodes are created in the context's node document.

public DocumentFragment ParseFragment(string html, Element context)

Parameters

html string
context Element

Returns

DocumentFragment