将YAML文件读入C#?

时间:2017-10-23 19:13:57

标签: c# yaml

我正在尝试将.yaml文件读入我的C#应用​​程序,并将数据存储在自定义类中。我正在尝试使用来自nuget的YamlDotNet

文件粘贴在下面,我的代码(来自YamlDotNet示例)是:

   public void readMapFile(string filepath)
        {
            // Setup the input
            var input = new StringReader(filepath);

            // Load the stream
            var yaml = new YamlStream();
            yaml.Load(input);

            // Examine the stream
            var mapping =
                (YamlMappingNode)yaml.Documents[0].RootNode;

            foreach (var entry in mapping.Children)
            {
                Console.WriteLine(((YamlScalarNode)entry.Key).Value);
            }

            // List all the items
            var items = (YamlSequenceNode)mapping.Children[new YamlScalarNode("aruco_bc_markers")];

           }
    }

然而,这崩溃了,给我一个错误:

  

未处理的异常:System.InvalidCastException:无法转换类型为' YamlDotNet.RepresentationModel.YamlScalarNode'的对象输入' YamlDotNet.RepresentationModel.YamlMappingNode' .`

我是这种文件阅读的新手,我被卡住了。我哪里错了?谢谢。

%YAML:1.0
---
aruco_bc_dict: ARUCO
aruco_bc_nmarkers: 24
aruco_bc_mInfoType: 1
aruco_bc_markers:
   - { id:0, corners:[ [ -1.2928584814071655e+00, 8.1286805868148804e-01,
       -1.6458697617053986e-01 ], [ -1.1746160984039307e+00,
       8.1223398447036743e-01, -1.4413379132747650e-01 ], [
       -1.1754947900772095e+00, 6.9224494695663452e-01,
       -1.4277370274066925e-01 ], [ -1.2937371730804443e+00,
       6.9287902116775513e-01, -1.6322688758373260e-01 ] ] }
   - { id:1, corners:[ [ -7.9834830760955811e-01, 8.1106305122375488e-01,
       -9.9434338510036469e-02 ], [ -6.7920655012130737e-01,
       8.1078404188156128e-01, -8.5110619664192200e-02 ], [
       -6.7947661876678467e-01, 6.9078433513641357e-01,
       -8.5201270878314972e-02 ], [ -7.9861837625503540e-01,
       6.9106334447860718e-01, -9.9524989724159241e-02 ] ] }
   - { id:2, corners:[ [ -3.0384334921836853e-01, 8.1034839153289795e-01,
       -3.8991540670394897e-02 ], [ -1.8399941921234131e-01,
       8.1008774042129517e-01, -3.2878942787647247e-02 ], [
       -1.8429389595985413e-01, 6.9008994102478027e-01,
       -3.2222278416156769e-02 ], [ -3.0413782596588135e-01,
       6.9035059213638306e-01, -3.8334876298904419e-02 ] ] }

1 个答案:

答案 0 :(得分:4)

第一个问题是你正在读取文件名本身作为yaml文件,而不是文件内容。使用StreamReader代替StringReader

using (var reader = new StreamReader(filepath)) {
    // Load the stream
    var yaml = new YamlStream();
    yaml.Load(reader);
    // the rest
}

然后,YAML 1.0非常老,所以似乎解析器不理解%YAML: 1.0指令(在以后的版本中,“:”已从该指令中删除)。所以删除它和“---”(不需要)。您可以将其更改为%YAML 1.1(不是“:”,版本1.1,1.0被解析器拒绝),而不是删除。

然后,意图很重要。此版本(注意“id”和“corner”中“:”之后的空格)将使用您的代码进行解析而不会出现问题:

%YAML 1.1
---
aruco_bc_dict: ARUCO
aruco_bc_nmarkers: 24
aruco_bc_mInfoType: 1
aruco_bc_markers:
   - { id: 0, corners: [ [ -1.2928584814071655e+00, 8.1286805868148804e-01,
       -1.6458697617053986e-01 ], [ -1.1746160984039307e+00,
       8.1223398447036743e-01, -1.4413379132747650e-01 ], [
       -1.1754947900772095e+00, 6.9224494695663452e-01,
       -1.4277370274066925e-01 ], [ -1.2937371730804443e+00,
       6.9287902116775513e-01, -1.6322688758373260e-01 ] ] }
   - { id: 1, corners: [ [ -7.9834830760955811e-01, 8.1106305122375488e-01,
       -9.9434338510036469e-02 ], [ -6.7920655012130737e-01,
       8.1078404188156128e-01, -8.5110619664192200e-02 ], [
       -6.7947661876678467e-01, 6.9078433513641357e-01,
       -8.5201270878314972e-02 ], [ -7.9861837625503540e-01,
       6.9106334447860718e-01, -9.9524989724159241e-02 ] ] }
   - { id: 2, corners: [ [ -3.0384334921836853e-01, 8.1034839153289795e-01,
       -3.8991540670394897e-02 ], [ -1.8399941921234131e-01,
       8.1008774042129517e-01, -3.2878942787647247e-02 ], [
       -1.8429389595985413e-01, 6.9008994102478027e-01,
       -3.2222278416156769e-02 ], [ -3.0413782596588135e-01,
       6.9035059213638306e-01, -3.8334876298904419e-02 ] ] }