gpt4 book ai didi

c# - 将 YAML 文件读入 C#?

转载 作者:行者123 更新时间:2023-11-30 14:47:01 34 4
gpt4 key购买 nike

我正在尝试将 .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")];

}
}

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

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'YamlDotNet.RepresentationModel.YamlScalarNode' to type '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 ] ] }

最佳答案

第一个问题是您将文件名本身读取为 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”和“角”中“:”后的空格)将与您的代码一起解析而不会出现问题:

%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 ] ] }

关于c# - 将 YAML 文件读入 C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46896848/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com