I'm using YamlDotNet with an Azure Function v2 to serialise YAML from a markdown file (hosted on GitHub) to a .net object. I'm struggling with this error when attempting to deserialize the YAML string
Expected 'StreamEnd', got 'DocumentStart
I'm getting the markdown file content using HttpClient
with a GET request to https://github.com/martinkearn/Content/raw/fd83bf8218b7c5e01f8b498e8a831bcd3fc3c961/Blogs/Test.md which returns a raw markdown file in the response body.
My Model is
public class Article
{
public string Title { get; set; }
public string Author { get; set; }
public List<string> Categories { get; set; }
}
My YAML is
---
title: Test File
author: Martin Kearn
categories:
- Test
- GitHubCMS
- One More Tag
- another tag
---
Here is my code
// get url from request body
var url = "https://raw.githubusercontent.com/martinkearn/Content/fd83bf8218b7c5e01f8b498e8a831bcd3fc3c961/Blogs/Test.md";
// get raw file and extract YAML
using (var client = new HttpClient())
{
//setup HttpClient
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Add("User-Agent", "ExtractYAML Function");
//setup httpContent object
var response = await client.GetAsync(url);
string rawFile = await response.Content.ReadAsStringAsync();
using (var r = new StringReader(rawFile))
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.Build();
//This line is causing Expected 'StreamEnd', got 'DocumentStart'
var article = deserializer.Deserialize<Article>(r);
}
}
Your actual downloaded file contains:
The
---
is the end-of-directives marker, which is optional if you don't have any directives (%YAML 1.2
,%TAG ....
).Since you have an empty line after the second directive, this is counted as if your second document contained
You should at least get rid of that empty line and possible remove the second end-of-directives marker, putting the comment at the end of the first document
The end-of-document indicator in YAML is
...
at the beginning of a line.Make your file read:
or at most: