5

When I do :

var root = new YamlMappingNode();
var doc = new YamlDocument(root);
root.Add("one", "two");
var stream = new YamlStream(doc);
var buffer = new StringBuilder();
using (var writer = new StringWriter(buffer))
{
  stream.Save(writer, false);
  var t = buffer.ToString();
}

I get :

one: two
...

Why is there 3 dots at the end of the file ?

Dragouf
  • 4,676
  • 4
  • 48
  • 55
  • ok, it seems to be part of the specs. Is there a way to don't append it at the end of the file ? – Dragouf Jun 15 '18 at 15:35
  • YamlStream is used for streaming multiple yaml files down a single stream. Was this intended, or are you just setting out to save a yaml file? – spender Jun 15 '18 at 15:46
  • it was not intended. I wanted to save a single file in memory to a string – Dragouf Jun 15 '18 at 15:47

1 Answers1

4

So YamlStream is for streaming multiple yaml documents down a single stream, therefore it codifies markers to indicate both end-of-file (---) and end-of-stream (...). If you're only serializing a single document, you probably don't want this.

Instead, use Serializer to write a node to a StreamWriter (backed-off by a (File)Stream):

var serializer = new Serializer(); //YamlDotNet.Serialization.Serializer
using (var fs = File.OpenWrite("some/path.yaml"))
using (var sw = new StreamWriter(fs))
{
    serializer.Serialize(sw, doc.RootNode);
}
spender
  • 117,338
  • 33
  • 229
  • 351