Generate Yaml document dynamically from C #

Is it possible to dynamically build a Yaml document from C # using Yaml.DotNet or another library?

I understand how this can be done using serialization, however, this requires a start from the structure of the object.

I am looking for a way to create Yaml document nodes on the go, as you would with Xml, using the XElement.Add (object) method, for example.

+6
source share
2 answers

You can do this using YamlDotNet. You start by creating a YamlStream , add one or more documents to it, you can add sequences , collations, and scalars .

Here is an example of how to do this :

var address = new YamlMappingNode( new YamlScalarNode("street"), new YamlScalarNode("123 Tornado Alley\nSuite 16") { Style = YamlDotNet.Core.ScalarStyle.Literal }, new YamlScalarNode("city"), new YamlScalarNode("East Westville"), new YamlScalarNode("state"), new YamlScalarNode("KS") ) { Anchor = "main-address" }; var stream = new YamlStream( new YamlDocument( new YamlMappingNode( new YamlScalarNode("repeipt"), new YamlScalarNode("Oz-Ware Purchase Invoice"), new YamlScalarNode("date"), new YamlScalarNode("2007-08-06"), new YamlScalarNode("customer"), new YamlMappingNode( new YamlScalarNode("given"), new YamlScalarNode("Dorothy"), new YamlScalarNode("family"), new YamlScalarNode("Gale") ), new YamlScalarNode("items"), new YamlSequenceNode( new YamlMappingNode( new YamlScalarNode("part_no"), new YamlScalarNode("A4786"), new YamlScalarNode("descrip"), new YamlScalarNode("Water Bucket (Filled)"), new YamlScalarNode("price"), new YamlScalarNode("1.47"), new YamlScalarNode("quantity"), new YamlScalarNode("4") ), new YamlMappingNode( new YamlScalarNode("part_no"), new YamlScalarNode("E1628"), new YamlScalarNode("descrip"), new YamlScalarNode("High Heeled \"Ruby\" Slippers"), new YamlScalarNode("price"), new YamlScalarNode("100.27"), new YamlScalarNode("quantity"), new YamlScalarNode("1") ) ), new YamlScalarNode("bill-to"), address, new YamlScalarNode("ship-to"), address, new YamlScalarNode("specialDelivery"), new YamlScalarNode("Follow the Yellow Brick\n" + "Road to the Emerald City.\n" + "Pay no attention to the\n" + "man behind the curtain.") { Style = YamlDotNet.Core.ScalarStyle.Literal } ) ) ); 
+3
source

Now I have developed how to do this using Yaml.Net. To start YamlStream, you need to load the source content using the Load () method.

 const string initialContent = "---\nversion: 1\n..."; var sr = new StringReader(initialContent); var stream = new YamlStream(); stream.Load(sr); 

You can then drop the RootNode of the YamlDocument to the YamlMappingNode method, which has the Add method.

 var rootMappingNode = (YamlMappingNode)stream.Documents[0].RootNode; rootMappingNode.Add("shout", "yay!"); 

Then you can add various types of node:

 var props = new YamlMappingNode(); props.Add("prop1", "value1"); props.Add("prop2", "value2"); rootMappingNode.Add("itemWithProps", props); var props2 = new YamlMappingNode(); props2.Add("prop1", "value1"); props2.Add("prop2", "value2"); var props3 = new YamlMappingNode(); props3.Add("prop1", "value1"); props3.Add("prop2", "value2"); var seq = new YamlSequenceNode(); seq.Add(props2); seq.Add(props3); rootMappingNode.Add("sequenceOfItems", seq); var col = new YamlSequenceNode(); col.Style = SequenceStyle.Flow; col.Add("a"); col.Add("b"); col.Add("c"); var seqMapping = new YamlMappingNode(); seqMapping.Add("collection", col); seq.Add(seqMapping); using (TextWriter writer = File.CreateText("C:\\temp\\test.yaml")) stream.Save(writer, false); 

Exit from this example:

 version: 1 shout: yay! itemWithProps: prop1: value1 prop2: value2 sequenceOfItems: - prop1: value1 prop2: value2 - prop1: value1 prop2: value2 - collection: [a, b, c] ... 

Thanks to @Antoine Aubry for creating Yaml.Net and vaguely pointing me in the right direction.

+2
source

All Articles