YamlDotNet “Expected 'StreamEnd', got 'DocumentStart'”when reading string










0















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











share|improve this question


























    0















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











    share|improve this question
























      0












      0








      0








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











      share|improve this question














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








      yaml azure-functions yamldotnet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 15:23









      Martin KearnMartin Kearn

      1,34211232




      1,34211232






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Your actual downloaded file contains:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          ---

          # Test file


          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



          ---
          null
          # Test file


          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:



          title: Test File 
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file


          or at most:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file





          share|improve this answer























          • Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

            – Martin Kearn
            Nov 12 '18 at 16:53











          • I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

            – Martin Kearn
            Nov 12 '18 at 17:00











          • I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

            – Anthon
            Nov 13 '18 at 6:02











          • OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

            – Martin Kearn
            Nov 13 '18 at 12:07










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265210%2fyamldotnet-expected-streamend-got-documentstartwhen-reading-string%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your actual downloaded file contains:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          ---

          # Test file


          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



          ---
          null
          # Test file


          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:



          title: Test File 
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file


          or at most:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file





          share|improve this answer























          • Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

            – Martin Kearn
            Nov 12 '18 at 16:53











          • I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

            – Martin Kearn
            Nov 12 '18 at 17:00











          • I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

            – Anthon
            Nov 13 '18 at 6:02











          • OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

            – Martin Kearn
            Nov 13 '18 at 12:07















          0














          Your actual downloaded file contains:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          ---

          # Test file


          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



          ---
          null
          # Test file


          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:



          title: Test File 
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file


          or at most:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file





          share|improve this answer























          • Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

            – Martin Kearn
            Nov 12 '18 at 16:53











          • I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

            – Martin Kearn
            Nov 12 '18 at 17:00











          • I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

            – Anthon
            Nov 13 '18 at 6:02











          • OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

            – Martin Kearn
            Nov 13 '18 at 12:07













          0












          0








          0







          Your actual downloaded file contains:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          ---

          # Test file


          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



          ---
          null
          # Test file


          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:



          title: Test File 
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file


          or at most:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file





          share|improve this answer













          Your actual downloaded file contains:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          ---

          # Test file


          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



          ---
          null
          # Test file


          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:



          title: Test File 
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file


          or at most:



          ---
          title: Test File
          author: Martin Kearn
          categories:
          - Test
          - GitHubCMS
          - One More Tag
          - another tag
          # Test file






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 15:53









          AnthonAnthon

          29.3k1693145




          29.3k1693145












          • Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

            – Martin Kearn
            Nov 12 '18 at 16:53











          • I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

            – Martin Kearn
            Nov 12 '18 at 17:00











          • I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

            – Anthon
            Nov 13 '18 at 6:02











          • OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

            – Martin Kearn
            Nov 13 '18 at 12:07

















          • Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

            – Martin Kearn
            Nov 12 '18 at 16:53











          • I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

            – Martin Kearn
            Nov 12 '18 at 17:00











          • I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

            – Anthon
            Nov 13 '18 at 6:02











          • OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

            – Martin Kearn
            Nov 13 '18 at 12:07
















          Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

          – Martin Kearn
          Nov 12 '18 at 16:53





          Thank you …. I want to use YAML as a metadata block at the start of a Markdown file which will have other Markdown content in it (GitHub flavoured Markdown). What is the correct way to structure this? I've updated the file listed in the regional question to show more markdown to show what I mean

          – Martin Kearn
          Nov 12 '18 at 16:53













          I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

          – Martin Kearn
          Nov 12 '18 at 17:00





          I've managed to get it working by chopping off the markdown content and leaving just eth yaml which can then be serialised using var yaml = response.Substring(0, response.LastIndexOf("---")); … is this what I need to do? Does YamlDotNet not handle documents that contain things other than YAML?

          – Martin Kearn
          Nov 12 '18 at 17:00













          I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

          – Anthon
          Nov 13 '18 at 6:02





          I don't know about YamlDotnet, but every other parser that I have used needs to be presented with A) just YAML B) needs to be told up front whether one, or multiple documents are available in the stream. They all require splitting of a YAML "header" and then parsing just the header

          – Anthon
          Nov 13 '18 at 6:02













          OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

          – Martin Kearn
          Nov 13 '18 at 12:07





          OK, thank you. between your main answer and the comments thread, we've got the answer I think; The parser needs to be presented with just YAML and it should be valid YAML without --- in the wrong place. I can't really mark your answer as 'the' answer as it does not tell the full story to anyone from the future. If you can update it to include the details about only presenting YAML and perhaps include the line of code I mentioned, I'll mark it as the answer. Thank you

          – Martin Kearn
          Nov 13 '18 at 12:07

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265210%2fyamldotnet-expected-streamend-got-documentstartwhen-reading-string%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo