Learning to Deserialize YAML in C# with YamlDotNET

preview_player
Показать описание
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.

### Example

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

```yml
user:
name: "World of Zero"
```

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

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

```sh
dotnet add package YamlDotNet
```

or via the Package Manager with

```sh
Install-Package YamlDotNet
```

***

***

Рекомендации по теме
Комментарии
Автор

Thank you, guy. This has helped me, it was exactly what was missing (camel casing convention) for my program to work

DanMunteanuOne
Автор

Would this work with the .NET Core too?

For me it says it cannot find a definition for

ozgurer
Автор

YAML = YAML Ain't Markup Language

hom-sha-bom
visit shbcf.ru