Github地址
按照md文件,使用方法如下:
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
...
var yml = @"
name: George Washington
age: 89
height_in_inches: 5.75
addresses:
home:
street: 400 Mockingbird Lane
city: Louaryland
state: Hawidaho
zip: 99970
";
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml
.Build();
//yml contains a string containing your YAML
var p = deserializer.Deserialize<Person>(yml);
var h = p.Addresses["home"];
System.Console.WriteLine($"{p.Name} is {p.Age} years old and lives at {h.Street} in {h.City}, {h.State}.");
但是我按照这种方式修改为读取文件后解析一直报错“Property not found”
最后发现是名称匹配方式UnderscoredNamingConvention的问题,翻译为“强调命名约定”,目前也没找到这种类型到底是什么。而我使用的驼峰命名法
,应该修改为PascalCaseNamingConvention.Instance
示例配置:
ViewMapArea:
StartPositionX: 10
StartPositionY: 30
AreaWidth: 50
AreaHeight: 80
示例代码:
private void ReadDefaultConfig()
{
//string defaultFilePath = Path.Combine(Application.StartupPath, "MapDefaultConfig.yml");
string defaultFilePath = Path.Combine(Application.StartupPath, "Test.yml");
try
{
string input = File.ReadAllText(defaultFilePath);
var deserializer = new DeserializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
//.IgnoreUnmatchedProperties() //不匹配的忽略
.Build();
//读取区域信息
Test viewMap = deserializer.Deserialize<Test>(input);
}
catch
{
throw;
}
}
如果觉得文章对你有帮助的话,留个赞再走吧,非常感谢!!!