使用 C# 读取 Yamldotnet
Reading Yamldotnet with c#
当我尝试使用 C# 读取 Yaml 文件并需要帮助来完成此任务时遇到一些问题,我如何将这样的 Yaml 文件读入变量以便我可以处理它们。
FileConfig:
sourceFolder: /home
destinationFolder: /home/billy/my-test-case
scenarios:
- name: first-scenario
alterations:
- tableExtension: ln
alterations:
- type: copy-line
sourceLineIndex: 0
destinationLineIndex: 0
- type: cell-change
sourceLineIndex: 0
columnName: FAKE_COL
newValue: NEW_Value1
- tableExtension: env
alterations:
- type: cell-change
sourceLineIndex: 0
columnName: ID
newValue: 10
这是我的代码
string text = System.IO.File.ReadAllText(@"test.yaml");
var deserializer = new Deserializer();
var result = deserializer.Deserialize<List<Hashtable>>(new StringReader(text));
/*foreach (var item in result)
{
Console.WriteLine("Item:");
Console.WriteLine(item.GetType());
foreach (DictionaryEntry entry in item)
{
Console.WriteLine("- {0} = {1}", entry.Key, entry.Value);
}
} */
最简单的方法是创建文档的 C# 模型。然后,您可以使用 Deserializer
将文档中存在的数据填充到该模型中。您的文档的可能模型是:
public class MyModel
{
[YamlMember(Alias = "FileConfig", ApplyNamingConventions = false)]
public FileConfig FileConfig { get; set; }
}
public class FileConfig
{
public string SourceFolder { get; set; }
public string DestinationFolder { get; set; }
public List<Scenario> Scenarios { get; set; }
}
public class Scenario
{
public string Name { get; set; }
public List<Alteration> Alterations { get; set; }
}
public class Alteration
{
public string TableExtension { get; set; }
public List<TableAlteration> Alterations { get; set; }
}
public class TableAlteration
{
public string Type { get; set; }
public int SourceLineIndex { get; set; }
public int DestinationLineIndex { get; set; }
public string ColumnName { get; set; }
public string NewValue { get; set; }
}
您可以反序列化为该模型,如下所示:
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var obj = deserializer.Deserialize<MyModel>(yaml);
您可以在此处 运行 此代码:https://dotnetfiddle.net/SRABFM
当然,我在这里建议的模型是非常幼稚的,如果对你的领域模型有更多的了解,你一定能想出一个更好的模型。
当我尝试使用 C# 读取 Yaml 文件并需要帮助来完成此任务时遇到一些问题,我如何将这样的 Yaml 文件读入变量以便我可以处理它们。
FileConfig:
sourceFolder: /home
destinationFolder: /home/billy/my-test-case
scenarios:
- name: first-scenario
alterations:
- tableExtension: ln
alterations:
- type: copy-line
sourceLineIndex: 0
destinationLineIndex: 0
- type: cell-change
sourceLineIndex: 0
columnName: FAKE_COL
newValue: NEW_Value1
- tableExtension: env
alterations:
- type: cell-change
sourceLineIndex: 0
columnName: ID
newValue: 10
这是我的代码
string text = System.IO.File.ReadAllText(@"test.yaml");
var deserializer = new Deserializer();
var result = deserializer.Deserialize<List<Hashtable>>(new StringReader(text));
/*foreach (var item in result)
{
Console.WriteLine("Item:");
Console.WriteLine(item.GetType());
foreach (DictionaryEntry entry in item)
{
Console.WriteLine("- {0} = {1}", entry.Key, entry.Value);
}
} */
最简单的方法是创建文档的 C# 模型。然后,您可以使用 Deserializer
将文档中存在的数据填充到该模型中。您的文档的可能模型是:
public class MyModel
{
[YamlMember(Alias = "FileConfig", ApplyNamingConventions = false)]
public FileConfig FileConfig { get; set; }
}
public class FileConfig
{
public string SourceFolder { get; set; }
public string DestinationFolder { get; set; }
public List<Scenario> Scenarios { get; set; }
}
public class Scenario
{
public string Name { get; set; }
public List<Alteration> Alterations { get; set; }
}
public class Alteration
{
public string TableExtension { get; set; }
public List<TableAlteration> Alterations { get; set; }
}
public class TableAlteration
{
public string Type { get; set; }
public int SourceLineIndex { get; set; }
public int DestinationLineIndex { get; set; }
public string ColumnName { get; set; }
public string NewValue { get; set; }
}
您可以反序列化为该模型,如下所示:
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var obj = deserializer.Deserialize<MyModel>(yaml);
您可以在此处 运行 此代码:https://dotnetfiddle.net/SRABFM
当然,我在这里建议的模型是非常幼稚的,如果对你的领域模型有更多的了解,你一定能想出一个更好的模型。