从 C# 动态构建 Yaml 文档
Build a Yaml document dynamically from c#
是否可以使用 Yaml.DotNet 或其他库从 c# 动态构建 Yaml 文档?
我了解如何使用序列化来完成此操作,但这需要从对象结构开始。
我正在寻找一种方法来动态创建 Yaml 文档节点,就像您使用 Xml 使用 XElement.Add(object) 方法一样。
您可以使用 YamlDotNet. You start by creating a YamlStream, add one or more document to it, then you can add sequences, mappings and scalars 来做到这一点。
Here is an example on how to do it:
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
}
)
)
);
我现在已经知道如何使用 Yaml.Net 来做到这一点。 YamlStream 需要使用 Load() 方法加载一些初始内容。
const string initialContent = "---\nversion: 1\n...";
var sr = new StringReader(initialContent);
var stream = new YamlStream();
stream.Load(sr);
然后您可以将 YamlDocument 的 RootNode 转换为具有 Add 方法的 YamlMappingNode。
var rootMappingNode = (YamlMappingNode)stream.Documents[0].RootNode;
rootMappingNode.Add("shout", "yay!");
然后您可以在保存之前添加各种节点类型:
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);
这个例子的输出是:
version: 1
shout: yay!
itemWithProps:
prop1: value1
prop2: value2
sequenceOfItems:
- prop1: value1
prop2: value2
- prop1: value1
prop2: value2
- collection: [a, b, c]
...
感谢@Antoine Aubry 创建了 Yaml.Net 并模糊地指出了正确的方向。
是否可以使用 Yaml.DotNet 或其他库从 c# 动态构建 Yaml 文档?
我了解如何使用序列化来完成此操作,但这需要从对象结构开始。
我正在寻找一种方法来动态创建 Yaml 文档节点,就像您使用 Xml 使用 XElement.Add(object) 方法一样。
您可以使用 YamlDotNet. You start by creating a YamlStream, add one or more document to it, then you can add sequences, mappings and scalars 来做到这一点。
Here is an example on how to do it:
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
}
)
)
);
我现在已经知道如何使用 Yaml.Net 来做到这一点。 YamlStream 需要使用 Load() 方法加载一些初始内容。
const string initialContent = "---\nversion: 1\n...";
var sr = new StringReader(initialContent);
var stream = new YamlStream();
stream.Load(sr);
然后您可以将 YamlDocument 的 RootNode 转换为具有 Add 方法的 YamlMappingNode。
var rootMappingNode = (YamlMappingNode)stream.Documents[0].RootNode;
rootMappingNode.Add("shout", "yay!");
然后您可以在保存之前添加各种节点类型:
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);
这个例子的输出是:
version: 1
shout: yay!
itemWithProps:
prop1: value1
prop2: value2
sequenceOfItems:
- prop1: value1
prop2: value2
- prop1: value1
prop2: value2
- collection: [a, b, c]
...
感谢@Antoine Aubry 创建了 Yaml.Net 并模糊地指出了正确的方向。