我有一组相当独特的结构化的数据。看起来像这样:
foo:
- name: "some name"
location: "some location"
type: "someType"
bar:
- name: "A bar element"
location: "location here"
type: "someOtherType"
attachments:
- type: "attachmentTypeA"
name: "Attachment name"
- type: "attachmentTypeB"
name: "Attachment name"
baz:
- name: "another name"
location: "another location"
type: "anotherType"
qux:
- name: "My name here"
location: "My location here"
type: "SomeOtherTypeHere"
xyzzy:
- name: "Another name here"
location: "Another location here"
type: "anotherTypeHere"
bar:
- name: "Some name here"
location: "Some location here"
type: "typeHere"
attachments:
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeB"
name: "attachment name here"
- name: "Another name here"
location: "Another location here"
type: "anotherTypeHere"
attachments:
- type: "attachmentTypeA"
name: "attachment name here"
- type: "attachmentTypeC"
name: "attachment name here"
- type: "attachmentTypeD"
name: "attachment name here"
- name: "Another baz listing"
location: "Baz location"
type: "bazTypeHere"
因此,基本上,您在顶层有“ foo”(并且可以有多个foo,但总是在顶层)。通常,结构为:
foo> baz> qux> xyzzy> bar
但是,任何子元素都可以位于根目录下,也可以位于foo下,只要它们顺序正确即可。所以这些是有效的:
foo
qux
xyzzy
bar
attachments
bar
attachments
是这样的:
foo
baz
qux
xyzzy
bar
attachments
bar
attachments
qux
xyzzy
bar
attachments
bar
attachments
xyzzy
bar
attachments
bar
attachments
以此类推。我知道这很古怪。但这就是我继承的数据集。我查看了示例,特别是DeserializeObjectGraph和LoadingAYamlStream示例。像这样布置数据时,DeserializeObjectGraph方法变得有些疯狂。我终于放弃了,因为它太毛茸茸了。我认为,流方法似乎更合适,但我遇到了麻烦。
我正在按如下方式加载YAML:
string contents = System.IO.File.ReadAllText ( fileName );
var input = new StringReader (contents);
var yaml = new YamlStream ();
yaml.Load (input);
如您所见,那里没有任何幻想。我只是想获得一个对象的“树”,然后可以对其进行迭代。我尝试从根节点使用AllNodes属性,但是我一辈子都无法弄清楚如何以某种有意义的方式递归地遍历它们。我还将承认我是一个仍在学习的C#n00btard(这里是C老人),所以请多包涵!
有人可以建议一种方法,或者可能有一些代码甚至是伪代码来帮助我吗?