Why isn't content written to yaml document?

My intention is to write an array of Items in a yaml file, format should be like this:

- country: Denmark
  city: Copenhagen
- country: Italy
  city: Rome

So I wrote this method:

public static void Write(MyItem myItem)
{
    var myFilePath = "myYamlDoc.yaml";

    var yamlStream = new YamlStream();

    var yamlNode = new YamlMappingNode
        {
            { "country", new YamlScalarNode(myItem.Country) { Style = ScalarStyle.Literal }},
            { "city", new YamlScalarNode(myItem.City) { Style = ScalarStyle.Literal }};
        var yamlDocument = new YamlDocument(yamlNode);
        yamlStream.Add(yamlDocument);
    
}

No errors, but the content isn't written/saved to the yaml document either... help?

Later edit:

public static void Write4()
        {
            var myFilePath = "myFile3.yaml";

            var items = new MyItem[]
                {
                    new MyItem()
                    {
                        Prop1= "A4786",
                        Prop2 = "Water Bucket (Filled)",
                        Prop3 = "X"
                    },
                    new MyItem()
                    {
                        Prop1= "A4786",
                        Prop2 = "Water Bucket (Filled)",
                        Prop3 = "X"
                    }
                };

            var serializer = new SerializerBuilder().Build();
            var yamlText = serializer.Serialize(items);

            var yamlStream = new YamlStream();

            using (StreamWriter writer = System.IO.File.Exists(myFilePath) ? System.IO.File.AppendText(myFilePath) : System.IO.File.CreateText(myFilePath))
            {
                writer.WriteLine(yamlText);
                yamlStream.Save(writer, false);
            }
}

Works like this but do you think it's the righy way to do it? In library they use Emitters? for the Save but the documentation is lacking or at least I dont find it. Is there a better yaml library I could use? Why not write directly to the yamlStream, I mean why use StreamWriter at all?

1 answer

  • answered 2020-09-14 07:39 jgauffin

    I'm assuming that you are using YamlDotnet. The class called YamlStream, isn't really a .NET stream (it doesn't inherit Stream). It's rather a YamlDocumentCollection.

    Therefore it will never save the documents to somewhere.

    If you look at your first code example you never use the filename and therefore the file is never saved to the disk.

    In your second example it works since the "YamlStream" can take a TextWriter derived class (in your case the StreamWriter) and use that to save the contents to disk.

    I would however written that code as:

    using (var fileStream = new FileStream(myFilePath, FileMode.Append, FileAccess.Write))
    {
        using (StreamWriter writer = new StreamWriter(fileStream))
        {
            writer.WriteLine(yamlText);
            yamlStream.Save(writer, false);
        }
    }
    

    As it makes the intent more clear (always append to the file). Otherwise just use FileMode.Create to always overwrite file (if it exists).