YamlElement Struct

A node of a YamlDocument: a scalar, a mapping, a sequence or an alias.

public readonly struct YamlElement : IEquatable<YamlElement>
Implements

Nested types

NameDescription
YamlElement.MappingEnumeratorEnumerates the entries of a mapping.
YamlElement.SequenceEnumeratorEnumerates the items of a sequence.

Properties

NameDescription
AnchorGets the anchor of the node, or the name an alias refers to.
EndGets the location of the end of the node, after its content.
IsFlowGets a value indicating whether the collection is written in the flow style.
IsNullGets a value indicating whether the node is a null of the core schema: an empty plain scalar, ~ or null, without tag.
KindGets the kind of the node.
ScalarStyleGets the style of the scalar.
StartGets the location of the start of the node, at its anchor or tag if any.
TagGets the tag of the node resolved with the %TAG directives, such as tag:yaml.org,2002:str.
Utf8LengthGets the length of the text of the node in YamlDocument.Utf8Text, in bytes, with its properties and its content.
Utf8OffsetGets the offset of the node in YamlDocument.Utf8Text, in bytes, at its anchor or tag if any.

Gets the anchor of the node, or the name an alias refers to.

public string? Anchor { get; }

Property value

string?

Gets the location of the end of the node, after its content.

public Mark End { get; }

Property value

Mark

Remarks

Gets a value indicating whether the collection is written in the flow style.

public bool IsFlow { get; }

Property value

bool

Gets a value indicating whether the node is a null of the core schema: an empty plain scalar, ~ or null, without tag.

public bool IsNull { get; }

Property value

bool

Gets the kind of the node.

public YamlElementKind Kind { get; }

Property value

YamlElementKind

ScalarStyle

YamlElement.cs

Gets the style of the scalar.

public ScalarStyle ScalarStyle { get; }

Property value

ScalarStyle

Gets the location of the start of the node, at its anchor or tag if any.

public Mark Start { get; }

Property value

Mark

Remarks

Mark.Index counts UTF-16 characters: it is the index in the string the document was parsed from, or in the decoded text of UTF-8 input. Line and column are 0-based.

Gets the tag of the node resolved with the %TAG directives, such as tag:yaml.org,2002:str.

public string? Tag { get; }

Property value

string?

Utf8Length

YamlElement.cs

Gets the length of the text of the node in YamlDocument.Utf8Text, in bytes, with its properties and its content.

public int Utf8Length { get; }

Property value

int

Utf8Offset

YamlElement.cs

Gets the offset of the node in YamlDocument.Utf8Text, in bytes, at its anchor or tag if any.

public int Utf8Offset { get; }

Property value

int

Remarks

The offset counts UTF-8 bytes, even when the document was parsed from a string. Use YamlElement.Start for a position in the string. The offset of a node of a YamlElement.Clone is the offset in the input of the original.

Indexers

NameDescription
this[int]Gets the item of a sequence.
this[string]Gets the value of a mapping entry whose key is the scalar key.

Item overloads

Gets the item of a sequence.

public YamlElement this[int index] { get; }
Parameters
index int
Property value

YamlElement

Exceptions
InvalidOperationException
The node is not a sequence.
ArgumentOutOfRangeException
index is outside the sequence.

this[string]

YamlElement.cs

Gets the value of a mapping entry whose key is the scalar key.

public YamlElement this[string key] { get; }
Parameters
key string
Property value

YamlElement

Exceptions
KeyNotFoundException
The mapping has no such key.

Methods

NameDescription
Clone()Gets a copy of the node that stays valid after its document is disposed, like JsonElement.Clone in System.Text.Json.
EnumerateMapping()Enumerates the entries of a mapping.
EnumerateSequence()Enumerates the items of a sequence.
Equals(YamlElement)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.
GetHashCode()Returns the hash code for this instance.
GetLength()Gets the number of items of a sequence, or of entries of a mapping.
GetRawText()Gets the text of the node in the input, with its properties and its content.
GetRawUtf8Text()Gets the text of the node in the input, byte for byte, without allocating.
GetString()Decodes the value of the scalar.
ResolveAlias()Gets the node an alias refers to: the last node before it with that anchor, in the same document.
ToString()Returns the value of a scalar, or the text of a collection.
TryGetBoolean(bool)Parses the plain scalar as a boolean of the core schema: true, True, TRUE, false, False, FALSE.
TryGetDouble(double)Parses the plain scalar as a floating-point number, including .inf, -.inf and .nan.
TryGetInt64(long)Parses the plain scalar as a 64-bit integer, in the decimal notation.
TryGetProperty(ReadOnlySpan<byte>, YamlElement)Finds the value of a mapping entry whose key is a scalar equal to a UTF-8 text, without allocating.
TryGetProperty(ReadOnlySpan<char>, YamlElement)Finds the value of a mapping entry whose key is a scalar equal to key, without allocating.
TryGetProperty(string, YamlElement)Finds the value of a mapping entry whose key is a scalar equal to key.
ValueEquals(ReadOnlySpan<byte>)Compares the value of the scalar with a UTF-8 text without allocating.
ValueEquals(ReadOnlySpan<char>)Compares the value of the scalar with a text without allocating.
ValueEquals(string)Compares the value of the scalar with a text without allocating.
WriteTo(Utf8YamlWriter)Writes the node and its descendants: their values, styles, anchors and tags.

Gets a copy of the node that stays valid after its document is disposed, like JsonElement.Clone in System.Text.Json.

public YamlElement Clone()

Returns

YamlElement The node in a document of its own, which owns its memory and does not need to be disposed.

Exceptions

ObjectDisposedException
The document of the node is disposed.

Remarks

The copy holds the text of the lines of the node and of its descendants, not the whole input. It has the same values, text, anchors and tags, and the same positions in the input: YamlElement.Start, YamlElement.End and YamlElement.Utf8Offset. An alias of the node still resolves to the node of its anchor: the anchored nodes that are not in the node are copied too.

A node of a copy is returned as is. A copy is another node: it is not equal to the original.

EnumerateMapping()

YamlElement.cs

Enumerates the entries of a mapping.

public YamlElement.MappingEnumerator EnumerateMapping()

Returns

YamlElement.MappingEnumerator

Exceptions

InvalidOperationException
The node is not a mapping.

EnumerateSequence()

YamlElement.cs

Enumerates the items of a sequence.

public YamlElement.SequenceEnumerator EnumerateSequence()

Returns

YamlElement.SequenceEnumerator

Exceptions

InvalidOperationException
The node is not a sequence.

Equals overloads

Equals(YamlElement)

YamlElement.cs

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

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

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

Equals(object?)

YamlElement.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.

GetHashCode()

YamlElement.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.

GetLength()

YamlElement.cs

Gets the number of items of a sequence, or of entries of a mapping.

public int GetLength()

Returns

int

Exceptions

InvalidOperationException
The node is not a collection.

GetRawText()

YamlElement.cs

Gets the text of the node in the input, with its properties and its content.

public string GetRawText()

Returns

string

GetRawUtf8Text()

YamlElement.cs

Gets the text of the node in the input, byte for byte, without allocating.

public ReadOnlySpan<byte> GetRawUtf8Text()

GetString()

YamlElement.cs

Decodes the value of the scalar.

public string GetString()

Returns

string

Exceptions

InvalidOperationException
The node is not a scalar.

ResolveAlias()

YamlElement.cs

Gets the node an alias refers to: the last node before it with that anchor, in the same document.

public YamlElement ResolveAlias()

Returns

YamlElement

Exceptions

InvalidOperationException
The node is not an alias, or its anchor is not defined.

ToString()

YamlElement.cs

Returns the value of a scalar, or the text of a collection.

public override string ToString()

Returns

string

TryGetBoolean(bool)

YamlElement.cs

Parses the plain scalar as a boolean of the core schema: true, True, TRUE, false, False, FALSE.

public bool TryGetBoolean(out bool value)

Parameters

value bool

Returns

bool

TryGetDouble(double)

YamlElement.cs

Parses the plain scalar as a floating-point number, including .inf, -.inf and .nan.

public bool TryGetDouble(out double value)

Parameters

value double

Returns

bool

TryGetInt64(long)

YamlElement.cs

Parses the plain scalar as a 64-bit integer, in the decimal notation.

public bool TryGetInt64(out long value)

Parameters

value long

Returns

bool

TryGetProperty overloads

TryGetProperty(ReadOnlySpan<byte>, YamlElement)

YamlElement.cs

Finds the value of a mapping entry whose key is a scalar equal to a UTF-8 text, without allocating.

public bool TryGetProperty(ReadOnlySpan<byte> utf8Key, out YamlElement value)
Parameters
utf8Key ReadOnlySpan<byte>
value YamlElement
Returns

bool

TryGetProperty(ReadOnlySpan<char>, YamlElement)

YamlElement.cs

Finds the value of a mapping entry whose key is a scalar equal to key, without allocating.

public bool TryGetProperty(ReadOnlySpan<char> key, out YamlElement value)
Parameters
key ReadOnlySpan<char>
value YamlElement
Returns

bool

TryGetProperty(string, YamlElement)

YamlElement.cs

Finds the value of a mapping entry whose key is a scalar equal to key.

public bool TryGetProperty(string key, out YamlElement value)
Parameters
key string
value YamlElement
Returns

bool

Exceptions
ArgumentNullException
key is null.

ValueEquals overloads

ValueEquals(ReadOnlySpan<byte>)

YamlElement.cs

Compares the value of the scalar with a UTF-8 text without allocating.

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

bool

ValueEquals(ReadOnlySpan<char>)

YamlElement.cs

Compares the value of the scalar with a text without allocating.

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

bool

Remarks

A text with an invalid UTF-16 sequence, such as a lone surrogate, is equal to no value.

ValueEquals(string)

YamlElement.cs

Compares the value of the scalar with a text without allocating.

public bool ValueEquals(string text)
Parameters
text string
Returns

bool

Exceptions
ArgumentNullException
text is null.

WriteTo(Utf8YamlWriter)

YamlElement.cs

Writes the node and its descendants: their values, styles, anchors and tags.

public void WriteTo(Utf8YamlWriter writer)

Parameters

writer Utf8YamlWriter
The destination.

Exceptions

ArgumentNullException
writer is null.
ArgumentException
A tag is neither !, a local tag nor a URI, and the writer has no %TAG directive whose prefix starts it: see Utf8YamlWriter.WriteTagDirective. YamlDocument.WriteTo writes the directives of the documents.

Remarks

A plain scalar stays plain, so it keeps its type. A quoted or block scalar may be written in another quoted style, and a folded scalar is written as a literal one. Comments and document markers are not written.

Operators

NameDescription
operator ==(YamlElement, YamlElement)
operator !=(YamlElement, YamlElement)

operator ==(YamlElement, YamlElement)

YamlElement.cs
public static bool operator ==(YamlElement left, YamlElement right)

Parameters

left YamlElement
right YamlElement

Returns

bool

No documentation is available.

operator !=(YamlElement, YamlElement)

YamlElement.cs
public static bool operator !=(YamlElement left, YamlElement right)

Parameters

left YamlElement
right YamlElement

Returns

bool

No documentation is available.