---
title: "Limit YAML file size in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/change-yaml-parser-input-size-limit/
description: "Set a byte limit before parsing a YAML file in C#. See how MaxInputBytes accepts a small file and rejects that same file under a tighter cap."
version: "1.2.1"
---

# Limit YAML file size 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/change-yaml-parser-input-size-limit/change-yaml-parser-input-size-limit.zip)

Set `MaxInputBytes` before parsing. Use `settings.yaml` from the zip: `name: web` and `port: 8080` on separate lines.

```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;

var limits = YamlLimits.Untrusted
    with { MaxInputBytes = 32 };
var opts = new YamlDocumentOptions
    { Limits = limits };
using var file = File.OpenRead(
    "settings.yaml");
using var doc =
    YamlDocument.Parse(file, opts);
long cap = limits.MaxInputBytes;
Console.WriteLine($"{cap} bytes: accepted");
```

[The example](https://recursiveai.org/oneyaml/csharp/cookbook/change-yaml-parser-input-size-limit/change-yaml-parser-input-size-limit.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

For the paste path, create `settings.yaml` next to `Program.cs` with `name: web` on the first line and `port: 8080` on the second.

If the file is larger than `MaxInputBytes`, `Parse` throws a `YamlException` with `Category` set to `Limit`.

Prints:

```text
32 bytes: accepted
```

## The full program

The download includes this `settings.yaml`:

```yaml
name: web
port: 8080
```

The program opens the same file twice: once with a 32-byte cap and once with an 8-byte cap.

```csharp
using OneYaml;

CheckFile(32);
CheckFile(8);

static void CheckFile(long maxBytes)
{
    var limits = YamlLimits.Untrusted with { MaxInputBytes = maxBytes };
    var options = new YamlDocumentOptions { Limits = limits };
    using var file = File.OpenRead("settings.yaml");
    try
    {
        using var doc = YamlDocument.Parse(file, options);
        Console.WriteLine($"{maxBytes} bytes: accepted");
    }
    catch (YamlException error) when (error.Category == YamlErrorCategory.Limit)
    {
        Console.WriteLine($"{maxBytes} bytes: {error.Category}");
    }
}
```

Prints:

```text
32 bytes: accepted
8 bytes: Limit
```

## Variations

For a 1 MiB cap, replace the `limits` declaration in the snippet at the top:

```csharp
var limits = YamlLimits.Untrusted
    with { MaxInputBytes = 1024 * 1024 };
```

## Related recipes

- [Parse untrusted YAML in C#](https://recursiveai.org/oneyaml/csharp/cookbook/untrusted-input/)
- [Detect a YAML file’s encoding in C#](https://recursiveai.org/oneyaml/csharp/cookbook/detect-yaml-encoding-from-stream/)

## The short answer

Set `MaxInputBytes` on `YamlLimits`, assign it to `YamlDocumentOptions.Limits`, and pass the open file stream to `YamlDocument.Parse`. Start from `YamlLimits.Untrusted` and use `with` to change only the byte cap. The preset’s other bounds still apply. The example uses a deliberately small cap so you can see a rejection without downloading a large file.

## How do I check YAML file size before reading the file in C#?

Pass a `FileStream` rather than calling `File.ReadAllText` first. For a seekable stream, OneYaml checks its length before reading it. That gives you the size check and parser entry point in one call. If you read the file into a string first, the parser can still reject it, but it can’t undo the allocation your code already made.

The cap is in bytes, not characters. OneYaml checks both the input as provided and its UTF-8 form. For a file in an encoding other than UTF-8, passing the check on the file size alone doesn’t guarantee that the decoded document fits the cap. That’s why it’s useful to keep the parser bound even when you already know the size of the file on disk.

## What happens if a YAML upload is too large?

For a non-seekable stream, such as a request body without a known length, OneYaml reads in pieces and stops once the input passes the cap. Your [server’s upload limit](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads) is separate: an application can reject a request at the HTTP boundary before YAML parsing ever starts. Use the parser cap for the body you actually hand to OneYaml, including inputs that don’t arrive over HTTP.

The `Limit` category also covers other bounds. If you report `Limit` to users, don’t assume every such error means “file too large.” A small file may still hit a depth, scalar size or alias expansion bound. `Default` doesn’t set a byte cap, while `Untrusted` starts at 64 MiB; choose a smaller cap if your app only expects short configuration files. See the [preset table](https://recursiveai.org/oneyaml/csharp/docs/limits/#the-presets) for the bounds you inherit.
