YamDocument 以 3 个点结尾的文本表示

YamDocument to text representation end with 3 dots

当我这样做时:

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

我得到:

one: two
...

为什么文件末尾有 3 个点?

所以 YamlStream 用于将多个 yaml 文档沿单个流传输,因此它编码标记以指示文件结尾 (---) 和流结尾 (...).如果您只是序列化单个文档,您可能不希望这样做。

相反,使用 Serializer 将节点写入 StreamWriter(由 (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);
}