简体   繁体   English

寻求使用C#读取.yaml文件的指导

[英]Seeking guidance reading .yaml files with C#

Two months later: The YAML (Eve Online blueprint.yaml) file I tried to parse changed a huge deal which also made it much easier to parse using de deserializer. 两个月后:我试图解析的YAML(Eve Online blueprint.yaml)文件发生了巨大变化,这也使得使用de deserializer更容易解析。 If someone (for whatever reason) would like to see the code, it's updated on https://github.com/hkraal/ParseYaml 如果某人(无论出于何种原因)想要查看代码,请在https://github.com/hkraal/ParseYaml上更新


Based on the comment of Steve Wellens I've adjusted the code to do less things at once. 根据Steve Wellens的评论,我已经调整了代码,以便一次做更少的事情。 It didn't matter in the error itself. 错误本身并不重要。 I've created another project (Example1) in my solution to test the actual example found on aaubry.net I referenced to earlier. 我在我的解决方案中创建了另一个项目(Example1)来测试我之前引用的aaubry.net上的实际示例。

It gave me the same error when using an "dynamic" key which lead to my current conclusion: There is a difference between: 当使用“动态”键导致我当前的结论时,它给了我同样的错误:两者之间存在差异:

items:
    - part_no:   A4786

and

items:
    part_no:   A4786

The first is being used in the example which I (wrongly) assumed I could apply to my .yaml file which is using the second syntax. 第一个是在我(错误地)假设我可以应用于使用第二种语法的.yaml文件的示例中使用的。

Now it remains to find out how I can get the 'child' elements of my key with the syntax used in my yaml file... 现在,我仍然需要了解如何使用我的yaml文件中使用的语法来获取密钥的“子元素”...


As C# is used at work I started thinking about a nice project to learn about various aspects of the language while having a direct goal to work towards. 当C#在工作中使用时,我开始考虑一个很好的项目来学习语言的各个方面,同时有一个直接的目标来努力。 However I'm hitting my first wall quite early in my project parsing a Yaml file. 但是我在解析Yaml文件的项目中很早就打到了我的第一个墙。 My goal is to create an List of YamlBlueprint objects as defined in YamlBlueprint.cs but I don't even get to the end of the Yaml file. 我的目标是创建YamlBlueprint.cs中定义的YamlBlueprint对象列表,但我甚至没有到达Yaml文件的末尾。

I've setup a testcase on github which demonstrates the problem: https://github.com/hkraal/ParseYaml 我在github上设置了一个测试用例来演示这个问题: https//github.com/hkraal/ParseYaml

The example on http://www.aaubry.net/page/YamlDotNet-Documentation-Loading-a-YAML-stream works up untill I want to loop trough the items. http://www.aaubry.net/page/YamlDotNet-Documentation-Loading-a-YAML-stream上的示例可以运行,直到我想循环这些项目。 Based on what I see I should be able to give myKey as parameter to the YamlScalarNode() to access the items below it. 基于我所看到的,我应该能够将myKey作为参数提供给YamlScalarNode()来访问它下面的项目。

var items = (YamlSequenceNode)mapping.Children[new YamlScalarNode(myKey)];

I'm gettting the following error if I do: 如果我这样做,我会得到以下错误:

An unhandled exception of type 'System.InvalidCastException' occurred in yamldotnet.exe
Additional information: Unable to cast object of type 'YamlDotNet.RepresentationModel.YamlMappingNode' to type 'YamlDotNet.RepresentationModel.YamlSequenceNode'.

When passing "items" as parameter to YamlScalarNode() it just complains about the item not being there which is to be expected. 当将“items”作为参数传递给YamlScalarNode()时,它只是抱怨项目不在那里是预期的。 As I'm not sure where my toughttrain is going wrong I would love a bit assistance on how to troubleshoot this further. 由于我不确定我的Toughttrain哪里出错了,我会对如何进一步排除故障有所帮助。

Your question has already been correctly answered, but I would like to point out that your approach is probably not the best one for parsing files. 您的问题已得到正确回答,但我想指出您的方法可能不是解析文件的最佳方法。 The YamlDotNet.RepresentationModel.* types offer an object model that directly represents the YAML stream and its various parts. YamlDotNet.RepresentationModel。*类型提供直接表示YAML流及其各个部分的对象模型。 This is useful if you are creating an application that processes or generates YAML streams. 如果要创建处理或生成YAML流的应用程序,这将非常有用。

When you want to read a YAML document into an object graph, the best approach is to use the Deserializer class. 当您想要将YAML文档读入对象图时,最好的方法是使用Deserializer类。 With it you can write your code as follows: 有了它,您可以编写如下代码:

using(var reader = File.OpenText("blueprints.yaml")
{
    var deserializer = new Deserializer();
    var blueprintsById = deserializer.Deserialize<Dictionary<int, YamlBlueprint>>(reader);

    // Use the blueprintsById variable
}

The only difference is that the Id property of the YamlBlueprint instances won't be set, but that's just a matter of adding this: 唯一的区别是YamlBlueprint实例的Id属性不会被设置,但这只是添加这个:

foreach(var entry in blueprintsById)
{
    entry.Value.Id = entry.Key;
}

You have too much stuff going on in one line of code. 你在一行代码中有太多的东西。 Create a new YamlScalarNode object in one line, access the array in another line, cast the resultant object in another line. 在一行中创建一个新的YamlScalarNode对象,在另一行中访问该数组,将结果对象转换为另一行。 That way, you'll narrow down the problem area to a single step. 这样,您就可以将问题区域缩小到一个步骤。

The message is telling you that you are retrieving a YamlMappingNode from the array but you are casting it to a YamlSequenceNode. 该消息告诉您正在从阵列中检索YamlMappingNode,但是您将其转换为YamlSequenceNode。 Which is not allowed since the two types are obviously not related. 这是不允许的,因为这两种类型显然没有关系。

Well that was kinda stupid... it's kind of hard to create an mapping of something which only contains one element. 嗯,这有点愚蠢......创建一个只包含一个元素的东西的映射很难。 I've edited the repo linked in the OP with an working example in case somebody runs into the same problem. 我已经编辑了OP中链接的repo,并提供了一个工作示例,以防有人遇到同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM