TomlElement Struct

A value of a TomlDocument: a table, an array, or a scalar.

public readonly struct TomlElement : IEquatable<TomlElement>
Implements

Nested types

NameDescription
TomlElement.ArrayEnumeratorEnumerates the elements of an array.
TomlElement.TableEnumeratorEnumerates the keys and values of a table.

Properties

NameDescription
IsArrayOfTablesGets a value indicating whether the element is an array of tables, defined by [[header]].
IsDottedKeyTableGets a value indicating whether the table is created by dotted keys, such as a in a.b = 1.
IsImplicitTableGets a value indicating whether the table has no header and is only created by the keys of its sub-tables, such as a in [a.b] or a.b = 1.
IsInlineTableGets a value indicating whether the element is an inline table, such as { a = 1 }.
ValueFlagsGets how the scalar value is written, such as a literal string or a hexadecimal integer.
ValueKindGets the kind of the element.

IsArrayOfTables

TomlElement.cs

Gets a value indicating whether the element is an array of tables, defined by [[header]].

public bool IsArrayOfTables { get; }

Property value

bool

IsDottedKeyTable

TomlElement.cs

Gets a value indicating whether the table is created by dotted keys, such as a in a.b = 1.

public bool IsDottedKeyTable { get; }

Property value

bool

IsImplicitTable

TomlElement.cs

Gets a value indicating whether the table has no header and is only created by the keys of its sub-tables, such as a in [a.b] or a.b = 1.

public bool IsImplicitTable { get; }

Property value

bool

IsInlineTable

TomlElement.cs

Gets a value indicating whether the element is an inline table, such as { a = 1 }.

public bool IsInlineTable { get; }

Property value

bool

ValueFlags

TomlElement.cs

Gets how the scalar value is written, such as a literal string or a hexadecimal integer.

public Utf8TomlTokenFlags ValueFlags { get; }

Property value

Utf8TomlTokenFlags

Gets the kind of the element.

public TomlValueKind ValueKind { get; }

Property value

TomlValueKind

Indexers

NameDescription
this[int]Gets the element at an index of the array. The elements are linked, so the cost is linear in index: prefer TomlElement.EnumerateArray.

Gets the element at an index of the array. The elements are linked, so the cost is linear in index: prefer TomlElement.EnumerateArray.

public TomlElement this[int index] { get; }

Parameters

index int

Property value

TomlElement

Exceptions

InvalidOperationException
The element is not an array.
ArgumentOutOfRangeException
index is outside the array.

Methods

NameDescription
Clone()Gets a copy of the element that does not depend on the document, so it can be used after the document is disposed.
EnumerateArray()Enumerates the elements of the array.
EnumerateTable()Enumerates the keys and values of the table, in the order of the document.
Equals(TomlElement)Indicates whether the current object is equal to another object of the same type.
Equals(object?)Indicates whether this instance and a specified object are equal.
GetArrayLength()Gets the number of elements of the array.
GetBoolean()Gets the value of a boolean.
GetDouble()Gets the value of a float, or of an integer converted to a Double.
GetHashCode()Returns the hash code for this instance.
GetInt64()Gets the value of an integer.
GetProperty(ReadOnlySpan<byte>)Gets the value of a key of the table.
GetProperty(string)Gets the value of a key of the table.
GetPropertyCount()Gets the number of keys of the table.
GetRawText()Gets the text of the element in the document: a value, an array or an inline table as written, or the header of a table.
GetRawUtf8Text()Gets the UTF-8 text of the element in the document. See TomlElement.GetRawText.
GetSourceSpan()Gets where the element is in the document. The offsets and columns count UTF-16 code units.
GetString()Gets the value of a string.
GetTomlDateTime()Gets the value of a date or a time.
ToString()Gets the text of a string, or the TOML text of other elements.
TryGetDecimal(decimal)Gets the value of a number as a Decimal, parsed from its text to keep its digits.
TryGetDouble(double)Gets the value of a float, or of an integer converted to a Double.
TryGetInt32(int)Gets the value of an integer that fits a Int32.
TryGetInt64(long)Gets the value of an integer.
TryGetProperty(ReadOnlySpan<byte>, TomlElement)Gets the value of a key of the table.
TryGetProperty(ReadOnlySpan<char>, TomlElement)Gets the value of a key of the table.
TryGetProperty(string, TomlElement)Gets the value of a key of the table.
TryGetTomlDateTime(TomlDateTime)Gets the value of a date or a time.
ValueEquals(ReadOnlySpan<byte>)Compares the value of a string to UTF-8 text, without allocating.
ValueEquals(ReadOnlySpan<char>)Compares the value of a string to text, without allocating.
ValueEquals(string?)Compares the value of a string to text, without allocating.
WriteTo(Utf8TomlWriter)Writes the element with a writer.

Gets a copy of the element that does not depend on the document, so it can be used after the document is disposed.

public TomlElement Clone()

EnumerateArray()

TomlElement.cs

Enumerates the elements of the array.

public TomlElement.ArrayEnumerator EnumerateArray()

Returns

TomlElement.ArrayEnumerator

Exceptions

InvalidOperationException
The element is not an array.

EnumerateTable()

TomlElement.cs

Enumerates the keys and values of the table, in the order of the document.

public TomlElement.TableEnumerator EnumerateTable()

Returns

TomlElement.TableEnumerator

Exceptions

InvalidOperationException
The element is not a table.

Equals overloads

Equals(TomlElement)

TomlElement.cs

Indicates whether the current object is equal to another object of the same type.

public bool Equals(TomlElement other)
Parameters
other TomlElement
An object to compare with this object.
Returns

bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

TomlElement.cs

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameters
obj object?
The object to compare with the current instance.
Returns

bool true if obj and this instance are the same type and represent the same value; otherwise, false.

GetArrayLength()

TomlElement.cs

Gets the number of elements of the array.

public int GetArrayLength()

Returns

int

Exceptions

InvalidOperationException
The element is not an array.

GetBoolean()

TomlElement.cs

Gets the value of a boolean.

public bool GetBoolean()

Returns

bool

Exceptions

InvalidOperationException
The element is not a boolean.

GetDouble()

TomlElement.cs

Gets the value of a float, or of an integer converted to a Double.

public double GetDouble()

Returns

double

Exceptions

InvalidOperationException
The element is not a number.

GetHashCode()

TomlElement.cs

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int A 32-bit signed integer that is the hash code for this instance.

GetInt64()

TomlElement.cs

Gets the value of an integer.

public long GetInt64()

Returns

long

Exceptions

InvalidOperationException
The element is not an integer.

GetProperty overloads

GetProperty(ReadOnlySpan<byte>)

TomlElement.cs

Gets the value of a key of the table.

public TomlElement GetProperty(ReadOnlySpan<byte> utf8PropertyName)
Parameters
utf8PropertyName ReadOnlySpan<byte>
Returns

TomlElement

Exceptions
InvalidOperationException
The element is not a table.
KeyNotFoundException
The table has no such key.

GetProperty(string)

TomlElement.cs

Gets the value of a key of the table.

public TomlElement GetProperty(string propertyName)
Parameters
propertyName string
Returns

TomlElement

Exceptions
InvalidOperationException
The element is not a table.
KeyNotFoundException
The table has no such key.

GetPropertyCount()

TomlElement.cs

Gets the number of keys of the table.

public int GetPropertyCount()

Returns

int

Exceptions

InvalidOperationException
The element is not a table.

GetRawText()

TomlElement.cs

Gets the text of the element in the document: a value, an array or an inline table as written, or the header of a table.

public string GetRawText()

Returns

string

Remarks

A table without a header, such as the root table or a table created by dotted keys, has no text.

GetRawUtf8Text()

TomlElement.cs

Gets the UTF-8 text of the element in the document. See TomlElement.GetRawText.

public ReadOnlySpan<byte> GetRawUtf8Text()

GetSourceSpan()

TomlElement.cs

Gets where the element is in the document. The offsets and columns count UTF-16 code units.

public TomlSourceSpan? GetSourceSpan()

Returns

TomlSourceSpan? The location of the value, or of the key of a table without a header; null for the root table.

GetString()

TomlElement.cs

Gets the value of a string.

public string GetString()

Returns

string

Exceptions

InvalidOperationException
The element is not a string.

GetTomlDateTime()

TomlElement.cs

Gets the value of a date or a time.

public TomlDateTime GetTomlDateTime()

Returns

TomlDateTime

Exceptions

InvalidOperationException
The element is not a date or a time.

ToString()

TomlElement.cs

Gets the text of a string, or the TOML text of other elements.

public override string ToString()

Returns

string

TryGetDecimal(decimal)

TomlElement.cs

Gets the value of a number as a Decimal, parsed from its text to keep its digits.

public bool TryGetDecimal(out decimal value)

Parameters

value decimal

Returns

bool

TryGetDouble(double)

TomlElement.cs

Gets the value of a float, or of an integer converted to a Double.

public bool TryGetDouble(out double value)

Parameters

value double

Returns

bool

TryGetInt32(int)

TomlElement.cs

Gets the value of an integer that fits a Int32.

public bool TryGetInt32(out int value)

Parameters

value int

Returns

bool

TryGetInt64(long)

TomlElement.cs

Gets the value of an integer.

public bool TryGetInt64(out long value)

Parameters

value long

Returns

bool

TryGetProperty overloads

TryGetProperty(ReadOnlySpan<byte>, TomlElement)

TomlElement.cs

Gets the value of a key of the table.

public bool TryGetProperty(ReadOnlySpan<byte> utf8PropertyName, out TomlElement value)
Parameters
utf8PropertyName ReadOnlySpan<byte>
value TomlElement
Returns

bool

Exceptions
InvalidOperationException
The element is not a table.

TryGetProperty(ReadOnlySpan<char>, TomlElement)

TomlElement.cs

Gets the value of a key of the table.

public bool TryGetProperty(ReadOnlySpan<char> propertyName, out TomlElement value)
Parameters
propertyName ReadOnlySpan<char>
value TomlElement
Returns

bool

Exceptions
InvalidOperationException
The element is not a table.

TryGetProperty(string, TomlElement)

TomlElement.cs

Gets the value of a key of the table.

public bool TryGetProperty(string propertyName, out TomlElement value)
Parameters
propertyName string
value TomlElement
Returns

bool

Exceptions
InvalidOperationException
The element is not a table.

TryGetTomlDateTime(TomlDateTime)

TomlElement.cs

Gets the value of a date or a time.

public bool TryGetTomlDateTime(out TomlDateTime value)

Parameters

value TomlDateTime

Returns

bool

ValueEquals overloads

ValueEquals(ReadOnlySpan<byte>)

TomlElement.cs

Compares the value of a string to UTF-8 text, without allocating.

public bool ValueEquals(ReadOnlySpan<byte> utf8Text)
Parameters
utf8Text ReadOnlySpan<byte>
Returns

bool

Exceptions
InvalidOperationException
The element is not a string.

ValueEquals(ReadOnlySpan<char>)

TomlElement.cs

Compares the value of a string to text, without allocating.

public bool ValueEquals(ReadOnlySpan<char> text)
Parameters
text ReadOnlySpan<char>
Returns

bool

Exceptions
InvalidOperationException
The element is not a string.

ValueEquals(string?)

TomlElement.cs

Compares the value of a string to text, without allocating.

public bool ValueEquals(string? text)
Parameters
text string?
Returns

bool

Exceptions
InvalidOperationException
The element is not a string.

WriteTo(Utf8TomlWriter)

TomlElement.Write.cs

Writes the element with a writer.

public void WriteTo(Utf8TomlWriter writer)

Parameters

writer Utf8TomlWriter
The writer.

Remarks

A table that is not inline, written where the writer expects a key, is written as key/value pairs followed by its sub-tables, with headers relative to the table. Otherwise the element is written as a value: a table as an inline table, and an array of tables as an array of inline tables.

The values are copied as written in the document, except the TOML 1.1 syntax, which is written as TOML 1.0: the keys of a table defined by dotted keys are written under a header, and strings with escape sequences are encoded again.

Operators

NameDescription
operator ==(TomlElement, TomlElement)Compares two elements.
operator !=(TomlElement, TomlElement)Compares two elements.

operator ==(TomlElement, TomlElement)

TomlElement.cs

Compares two elements.

public static bool operator ==(TomlElement left, TomlElement right)

Parameters

left TomlElement
right TomlElement

Returns

bool

operator !=(TomlElement, TomlElement)

TomlElement.cs

Compares two elements.

public static bool operator !=(TomlElement left, TomlElement right)

Parameters

left TomlElement
right TomlElement

Returns

bool