Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Class

YamlWriter

OfficeIMO.Yaml

Namespace
OfficeIMO.Yaml
Assembly
OfficeIMO.Yaml
Source revision
0eef6a5756b9 with working tree changes
Implements
IDisposable
Modifiers
sealed

A forward-only YAML emitter, modeled on Utf8JsonWriter. Calls describe the document as events (start and end of mappings and sequences, keys, scalars, aliases, anchors, tags, comments and document markers) and the writer lays them out as block or flow YAML.

Inheritance

  • object
  • YamlWriter

Remarks

Output is deterministic and always valid YAML for the calls made. Strings written with the default style are plain when no YAML 1.2 core or YAML 1.1 reader could take them for anything but a string; otherwise they are quoted, so yes, on, 012 or 1:20 stay strings for PyYAML and other YAML 1.1 readers. Multi-line strings become literal block scalars in block context. Empty collections are written as [] and {}. Writing allocates nothing per call beyond the pooled output buffer of the stream overload. A writer is not thread-safe. ends the last line and flushes; it never disposes the caller's stream or text writer.

Usage

Explore 2 API references to this type

This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.

Accepted by parameters 2

Constructors

YamlWriter 2 overloads
public YamlWriter(Stream utf8Stream, YamlWriterOptions? options = null) #

Creates a writer that encodes UTF-8 without a byte-order mark into a stream.

Parameters

utf8Stream System.IO.Stream required
A writable stream. It is not disposed by the writer.
options OfficeIMO.Yaml.YamlWriterOptions? = null optional
Layout settings, or null for the defaults.
public YamlWriter(TextWriter writer, YamlWriterOptions? options = null) #

Creates a writer over a caller-owned TextWriter.

Parameters

writer System.IO.TextWriter required
The destination. It is not disposed by the writer.
options OfficeIMO.Yaml.YamlWriterOptions? = null optional
Layout settings, or null for the defaults.

Methods

public void Dispose() #

Ends the last line, flushes and releases the pooled buffer. The destination stays open.

public void Flush() #

Flushes written text to the destination without ending the current line.

public void WriteAlias(string anchor) #

Writes an alias (*name) to an anchor written earlier in the document.

Parameters

anchor string required

Exceptions

  • ArgumentException – The name is not a valid anchor name.
public void WriteAnchor(string name) #

Sets the anchor (&name) of the next node.

Parameters

name string required

Exceptions

  • ArgumentException – The name is empty or contains spaces or , [ ] { }.
public void WriteComment(string text) #

Writes comment lines (# text) at the indentation of the next entry; each line of text becomes one comment. Comments cannot be written between a key and its value or inside a flow collection.

Parameters

text string required
public void WriteDocumentEnd() #

Ends the current document with an explicit ... marker. Starting the next document ends it implicitly.

Exceptions

  • InvalidOperationException – No complete document is open.
public void WriteDocumentStart() #

Starts a document with an explicit --- marker. The first document needs no call: writing its root starts it.

Exceptions

  • InvalidOperationException – A collection is open, or the current document has no root yet.
public void WriteEndMapping() #

Ends the innermost mapping.

Exceptions

  • InvalidOperationException – The innermost open collection is not a mapping, or its last key has no value.
public void WriteEndSequence() #

Ends the innermost sequence.

Exceptions

  • InvalidOperationException – The innermost open collection is not a sequence.
public void WriteKey(string key) #

Writes a string mapping key with the default style. Complex keys are written with the node methods at a key position.

Parameters

key string required

Exceptions

  • InvalidOperationException – The writer is not at a key position.
WriteScalar 4 overloads
public void WriteScalar(string? value, YamlStyle style = None) #

Writes a string scalar, or a null scalar when value is null. The writer uses style when it can represent the value at this position and otherwise the most conservative style that can; YamlStyle.None (the default) chooses as described on YamlWriter. A plain style is honored even when the text reads as another type, so a caller can write on on purpose.

Parameters

value string? required
style OfficeIMO.Yaml.YamlStyle = None optional
public void WriteScalar(long value) #

Writes an integer as a plain scalar.

Parameters

value long required
public void WriteScalar(double value) #

Writes a floating-point number as a plain scalar that YAML 1.2 and YAML 1.1 readers both read as the same float: 1.0, 2.5e-07, .inf, -.inf or .nan.

Parameters

value double required
public void WriteScalar(bool value) #

Writes true or false.

Parameters

value bool required
public void WriteStartMapping(YamlStyle style = Block) #

Starts a mapping. Its entries follow as alternating keys and values.

Parameters

style OfficeIMO.Yaml.YamlStyle = Block optional
YamlStyle.Block (the default) or YamlStyle.Flow; inside a flow collection every collection is flow.
public void WriteStartSequence(YamlStyle style = Block) #

Starts a sequence.

Parameters

style OfficeIMO.Yaml.YamlStyle = Block optional
YamlStyle.Block (the default) or YamlStyle.Flow; inside a flow collection every collection is flow.
public void WriteTag(string tag) #

Sets the tag of the next node, in resolved form as YamlNode.Tag reports it: tag:yaml.org,2002:str is written !!str, local tags such as !Ref as they are, and other URIs verbatim.

Parameters

tag string required