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?