---
title: "Edit a YAML multiline string in C# and keep its block style"
url: https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-multiline-block-scalar/
description: "Edit a YAML multiline string in C# with YamlDocument.Set. Keep its literal or folded block style and let OneYaml adjust the final newline marker."
version: "1.2.1"
---

# Edit a YAML multiline string in C# and keep its block style

**OneYaml** is a free, open-source YAML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-multiline-block-scalar/edit-yaml-multiline-block-scalar.zip)

`YamlDocument.Set` keeps the `|` or `>` block style if it can hold the new string.

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
using OneYaml;

using var doc = YamlDocument.Parse("""
    message: |-
      Hello
      world
    active: true
    """);
doc.Set("/message", "Ready\nGo\n");
Console.WriteLine(doc);
```

If the string cannot be represented in the original style, OneYaml changes the style so the new value can still be read back.

To edit a file instead of an inline string, see [how to load and save YAML in C#](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/).

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-multiline-block-scalar/edit-yaml-multiline-block-scalar.zip) and `dotnet run`.

The snippet prints a literal block with `|` instead of `|-`, because the new value ends with one newline:

```yaml
message: |
  Ready
  Go
active: true
```

## Variations

**Keep a folded block.** In the example, use these lines in place of the parse and edit calls. Keep `Console.WriteLine(doc);` to see the result.

```csharp
using var doc = YamlDocument.Parse("""
    message: >
      Hello
      world
    active: true
    """);
doc.Set("/message", "Ready to go");
```

```yaml
message: >-
  Ready to go
active: true
```

## Related recipes

- [Edit YAML without losing comments in C#](https://recursiveai.org/oneyaml/csharp/cookbook/edit-yaml-keep-comments/)
- [Set YAML indentation in C#](https://recursiveai.org/oneyaml/csharp/cookbook/set-yaml-indentation/)

## The short answer

Parse the YAML with `YamlDocument.Parse`, then set the key’s value with `doc.Set("/message", "Ready\nGo\n")`. Pass the string your program should read back, not a piece of YAML with its own `|` header and indentation. The existing literal block stays literal. OneYaml changes the suffix from `-` to no suffix so the updated value keeps its final newline. The neighboring `active` key is not rewritten.

## What is the difference between `|` and `>` in YAML multiline strings?

The `|` marker is **literal**: the break between `Ready` and `Go` stays a newline in the value. The `>` marker is **folded**: YAML usually reads breaks between ordinary lines of text as spaces; blank lines and more-indented lines have their own rules. Choose a literal block for text where line breaks matter, such as a script, and a folded block for prose you want to wrap in the file without inserting a break at every wrap in the string. OneYaml [keeps the existing style](https://recursiveai.org/oneyaml/csharp/docs/editing/#how-a-new-value-is-written) where the new value allows it; see the [YAML 1.2.2 rules](https://yaml.org/spec/1.2.2/#81-block-scalar-styles) for how each style reads.

## What do `-` and `+` mean after a YAML block marker?

Those suffixes control **chomping**, which is what YAML does with line breaks at the end of the block. They don’t change how YAML reads the breaks in the middle. For a nonempty block, `|-` or `>-` strips the final break and trailing blank lines. `|` or `>` keeps the final break but drops trailing blank lines. `|+` or `>+` keeps both. See the [chomping rules](https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator).

In the recipe, the original `|-` has no newline at the end of the decoded string. The last `\n` in the C# replacement asks for one, so the header becomes `|`. In the variation the new string has no final `\n`, so the folded header becomes `>-`. In both cases the suffix changes to fit the latest value; preserving block style does not mean keeping the whole header verbatim.
