---
title: "Read UTF-16 YAML from a stream in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/detect-yaml-encoding-from-stream/
description: "Read a UTF-16 YAML file from a C# stream without specifying an encoding. Keep accented and Japanese text intact."
version: "1.2.1"
---

# Read UTF-16 YAML from a stream in C#

**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/detect-yaml-encoding-from-stream/detect-yaml-encoding-from-stream.zip)

The zip’s UTF-16 `config.yaml` contains `label: Café 東京`.

```shell
dotnet add package OneYaml
```

Works on [.NET 8, 9, and 10](https://dotnet.microsoft.com/download/dotnet).

```csharp
using OneYaml;

using var input = File.OpenRead("config.yaml");
using var doc = YamlDocument.Parse(input);
Console.WriteLine(doc.Get<string>("/label"));
```

Prints the label:

```text
Café 東京
```

Invalid YAML throws `YamlException`.

Unzip [the example](https://recursiveai.org/oneyaml/csharp/cookbook/detect-yaml-encoding-from-stream/detect-yaml-encoding-from-stream.zip), then `dotnet run`.

## Variations

To send the same YAML to a tool that expects UTF-8, use `ToUtf8Bytes()`. This variation prints the decoded UTF-8 YAML rather than just its label:

```csharp
using System.Text;
using OneYaml;

using var input = File.OpenRead("config.yaml");
using var doc = YamlDocument.Parse(input);
Console.Write(Encoding.UTF8.GetString(doc.ToUtf8Bytes()));
```

```yaml
label: Café 東京
```

## Related recipes

- [Read YAML in C# without a class](https://recursiveai.org/oneyaml/csharp/cookbook/read-yaml-without-a-class/)
- [Read multiple YAML documents in C#](https://recursiveai.org/oneyaml/csharp/cookbook/kubernetes-multi-document/)

---

## The short answer

Open the YAML file with `File.OpenRead` and pass the resulting stream to `YamlDocument.Parse`. For the included UTF-16 sample, OneYaml reads the byte order mark before decoding. `doc.Get<string>("/label")` then returns the label as a C# string without losing the accents or Japanese characters.

## How do I read a UTF-16 YAML file in C# without specifying the encoding?

Start with a stream of bytes rather than text decoded with a guessed encoding. A byte order mark is a prefix that identifies an encoding and, for UTF-16, which byte comes first. The sample starts with `FF FE`, the [UTF-16 little-endian mark](https://www.unicode.org/faq/utf_bom.html#bom). Those bytes aren’t part of the `label` key or its value.

The [OneYaml editing guide](https://github.com/recursive-ai-oss/oneyaml-csharp/blob/7818048c4be434333767c47d66176fc626d1da8e/docs/editing.md#L333-L339) lists UTF-8, UTF-16, and UTF-32 detection from a byte order mark or the first characters. If you already have a correctly decoded C# string, parse that string instead. If the text contains replacement characters because it was decoded incorrectly, reopen the original bytes; parsing the string cannot recover them.

## What’s the difference between UTF-8 and UTF-16 when writing YAML back?

They represent the same characters with different bytes. Reading `Café 東京` into a C# string is the same task either way, but when you write the YAML byte stream back, choose the encoding the next reader expects. `ToBytes()` and `WriteTo(...)` retain the source encoding and byte order mark. Choose those when updating a file that another tool expects to stay UTF-16. Use `ToUtf8Bytes()` when the next tool expects UTF-8, as in the variation. Neither choice requires changing the YAML keys or values.
