This page looks best with JavaScript enabled

Learning to Deserialize YAML in C# with YamlDotNET

 ·   ·  ☕ 2 min read

YAML (Yet Another Markup Language) provides a way to store structured data in files. It can be used in similar ways to other JSON or XML files and is commonly used with Kubernetes and Docker Compose. In this video we’ll be taking a look at how you can deserialize and consume data from a YAML file and use that data in your .NET applications.

To do this we’ll be using a library called YamlDotNet. We’ll be working with a .NET console application, but this library can also be used in ASP.NET, Unity3D or other .NET projects. YamlDotNet provides a set of API’s for both serializing and deserializing YAML files, we’ll be using these to parse a tags.yml file we provide into C# objects so that we can interact with them in our code.

Example

If you want to parse a YAML file that looks like this:

1
2
user:
  name: "World of Zero"

You would use this in C# to create a Player object that represents the yml data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public static Player ParsePlayer(string ymlContents) {
  var deserializer = new DeserializerBuilder()
    .WithNamingConvention(CamelCaseNamingConvention.Instance)
    .Build();
  return deserializer.Deserialize<Player>(ymlContents);
}

public class Player {
  public User Username { get; set; } 
}

public class User {
  public string Name { get; set; }
}

Installing YamlDotNet

YamlDotNet is available on Nuget and can be installed from the dotnet command line using:

1
dotnet add package YamlDotNet

or via the Package Manager with

1
Install-Package YamlDotNet

Note: Make sure to check Nuget for the latest available version.


More about YamlDotNet on it’s GitHub page: https://github.com/aaubry/YamlDotNet
You can find other YamlDotNet code samples at: https://github.com/aaubry/YamlDotNet/wiki/Samples
If you’d like to use YamlDotNet to parse YAML in Unity3D you can use the free package from Unity’s Asset Store: https://assetstore.unity.com/packages/tools/integration/yamldotnet-for-unity-36292


Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u


Sam Wronski
WRITTEN BY
Sam Wronski
Maker of things, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!