TomlDocument Class

A read-only, high-performance representation of a TOML document, over its UTF-8 bytes.

public sealed class TomlDocument : IDisposable

Remarks

The document validates the whole TOML grammar, including the rules the Utf8TomlReader does not check: duplicate keys, tables defined twice, and inline tables or static arrays extended by other keys. A table defined in several parts of the document, such as [a], [b] and then [a.c], is one table.

The values are decoded when they are read, so parsing does not allocate per value. The document uses pooled memory: dispose it once its elements are no longer used. Use TomlElement.Clone to keep an element after that.

Implements

Properties

NameDescription
RootElementGets the root table of the document.
Utf8TextGets the UTF-8 text of the document.

RootElement

TomlDocument.cs

Gets the root table of the document.

public TomlElement RootElement { get; }

Property value

TomlElement

Gets the UTF-8 text of the document.

public ReadOnlyMemory<byte> Utf8Text { get; }

Property value

ReadOnlyMemory<byte>

Methods

NameDescription
Dispose()Returns the pooled memory of the document. Its elements cannot be used anymore.
Parse(Stream, TomlDocumentOptions)Parses a UTF-8 TOML document read from a stream.
Parse(ReadOnlyMemory<byte>, TomlDocumentOptions)Parses a UTF-8 TOML document. The bytes are not copied, so they must not change while the document is used.
Parse(string, TomlDocumentOptions)Parses a TOML document.
ParseAsync(Stream, TomlDocumentOptions, CancellationToken)Parses a UTF-8 TOML document read from a stream.
WriteTo(Utf8TomlWriter)Writes the document with a writer.

Returns the pooled memory of the document. Its elements cannot be used anymore.

public void Dispose()

Parse overloads

Parse(Stream, TomlDocumentOptions)

TomlDocument.cs

Parses a UTF-8 TOML document read from a stream.

public static TomlDocument Parse(Stream utf8Toml, TomlDocumentOptions options = default)
Parameters
utf8Toml Stream
The stream to read, up to its end.
options TomlDocumentOptions
The options of the parser.
Returns

TomlDocument

Exceptions
TomlException
The document is not valid TOML.

Parse(ReadOnlyMemory<byte>, TomlDocumentOptions)

TomlDocument.cs

Parses a UTF-8 TOML document. The bytes are not copied, so they must not change while the document is used.

public static TomlDocument Parse(ReadOnlyMemory<byte> utf8Toml, TomlDocumentOptions options = default)
Parameters
utf8Toml ReadOnlyMemory<byte>
The UTF-8 bytes of the document. A UTF-8 byte order mark at the start is skipped.
options TomlDocumentOptions
The options of the parser.
Returns

TomlDocument

Exceptions
TomlException
The document is not valid TOML.

Parse(string, TomlDocumentOptions)

TomlDocument.cs

Parses a TOML document.

public static TomlDocument Parse(string toml, TomlDocumentOptions options = default)
Parameters
toml string
The text of the document.
options TomlDocumentOptions
The options of the parser.
Returns

TomlDocument

Exceptions
TomlException
The document is not valid TOML, or contains an unpaired surrogate.

ParseAsync(Stream, TomlDocumentOptions, CancellationToken)

TomlDocument.cs

Parses a UTF-8 TOML document read from a stream.

net10.0

public static Task<TomlDocument> ParseAsync(Stream utf8Toml, TomlDocumentOptions options = default, CancellationToken cancellationToken = default)

net11.0

public static Task<TomlDocument> ParseAsync(Stream utf8Toml, TomlDocumentOptions options = default, CancellationToken cancellationToken = default)

Parameters

utf8Toml Stream
The stream to read, up to its end.
options TomlDocumentOptions
The options of the parser.
cancellationToken CancellationToken
A token to cancel the reading.

Returns

Task<TomlDocument>

Exceptions

TomlException
The document is not valid TOML.

WriteTo(Utf8TomlWriter)

TomlDocument.cs

Writes the document with a writer.

public void WriteTo(Utf8TomlWriter writer)

Parameters

writer Utf8TomlWriter
The writer.