Table of Contents

YamlNode Class

Definition

Namespace
Bodu.Text.Yaml.Nodes
Assembly
Bodu.Text.Yaml.dll
Package
Bodu.Text.Yaml 1.3.0
Source
YamlNode.cs

Represents a node in a mutable YAML document object model, in the manner of System.Text.Json.Nodes.JsonNode.

public abstract class YamlNode
Inheritance
YamlNode
Derived
Inherited Members
Extension Methods

Remarks

The node tree is fully mutable: mappings (YamlObject), sequences (YamlArray), and scalars (YamlValue) can be created, edited, and re-serialized.

A node may appear at most once in a tree: it belongs to at most one container, and appears in that container once. Adding, inserting, or assigning a node that already belongs to a container, including the container it is being added to, throws an InvalidOperationException; assigning a node over itself is the one exception. Adding a container to itself or to one of its own descendants, which would make the tree a cycle, throws an InvalidOperationException too. Removing or replacing a node, or clearing its container, detaches it, so that it can be added elsewhere.

Properties

this[int]

Gets or sets the child node at the specified sequence index.

public virtual YamlNode? this[int index] { get; set; }

Parameters

index int

The zero-based index.

Property Value

YamlNode

The child node, or null.

Exceptions

InvalidOperationException

The node is not a sequence, or the assigned node already belongs to a container or is this node or one of its ancestors.

this[string]

Gets or sets the child node mapped to the specified key.

public virtual YamlNode? this[string key] { get; set; }

Parameters

key string

The mapping key.

Property Value

YamlNode

The mapped node, or null.

Exceptions

InvalidOperationException

The node is not a mapping, or the assigned node already belongs to a container or is this node or one of its ancestors.

Methods

AsArray()

Returns this node as a YamlArray.

public YamlArray AsArray()

Returns

YamlArray

The node typed as a sequence.

Exceptions

InvalidOperationException

The node is not a sequence.

AsObject()

Returns this node as a YamlObject.

public YamlObject AsObject()

Returns

YamlObject

The node typed as a mapping.

Exceptions

InvalidOperationException

The node is not a mapping.

AsValue()

Returns this node as a YamlValue.

public YamlValue AsValue()

Returns

YamlValue

The node typed as a scalar.

Exceptions

InvalidOperationException

The node is not a scalar.

Parse(string)

Parses YAML text into a mutable node tree.

public static YamlNode? Parse(string yaml)

Parameters

yaml string

The YAML source.

Returns

YamlNode

The root node, or null when the document is empty/null.

Exceptions

ArgumentNullException

yaml is null.

YamlFormatException

The source is not valid YAML.

ToYamlString()

Serializes the node tree to YAML text.

public string ToYamlString()

Returns

string

The YAML representation of the node.

WriteTo(Utf8YamlWriter)

Writes this node to the given writer.

public abstract void WriteTo(Utf8YamlWriter writer)

Parameters

writer Utf8YamlWriter

The writer to emit into.

Applies to

ProductVersions
.NET8, 10