C# 使用YamlDotNet反序列化Yaml
我有一个Yaml文件: 还有一节课:C# 使用YamlDotNet反序列化Yaml,c#,.net,serialization,yamldotnet,C#,.net,Serialization,Yamldotnet,我有一个Yaml文件: 还有一节课: public class IconSearch { public string Name { get; set; } public string ClassName { get; set; } public IEnumerable<string> Filters { get; set; } } 公共类IconSearch { 公共字符串名称{get;set;} 公共字符串类名{get;set;} 公共IEnumer
public class IconSearch
{
public string Name { get; set; }
public string ClassName { get; set; }
public IEnumerable<string> Filters { get; set; }
}
公共类IconSearch
{
公共字符串名称{get;set;}
公共字符串类名{get;set;}
公共IEnumerable筛选器{get;set;}
}
您能告诉我如何将yaml反序列化为IEnumerable对象吗
我希望类似的东西可以工作,但它返回null——我猜这是因为我的一个属性不是根节点(图标)。相反,我正在尝试序列化根的子级
var input = new StringReader(reply);
var yaml = new YamlStream();
yaml.Load(input);
var icons = deserializer.Deserialize<IconSearch>(input);
var输入=新的StringReader(回复);
var yaml=新的YamlStream();
yaml.负载(输入);
变量图标=反序列化器。反序列化(输入);
您试图反序列化的类似乎缺少属性。
我将yaml转换为json再转换为csharp,这是生成的类:
public class Rootobject
{
public Icon[] icons { get; set; }
}
public class Icon
{
public string[] categories { get; set; }
public object created { get; set; }
public string[] filter { get; set; }
public string id { get; set; }
public string name { get; set; }
public string unicode { get; set; }
public string[] aliases { get; set; }
public string[] label { get; set; }
public string[] code { get; set; }
public string url { get; set; }
}
使用的资源:(我在visual studio中使用了Paste special) 使用此命令进行反序列化
var icons = deserializer.Deserialize<RootObject>(input);
var icons=反序列化程序。反序列化(输入);
更新我已经注释掉了您用来创建YamlStream的那一行,因为它不是必需的(它将读取器定位到流的末尾而不是开始,这可以解释为什么您之前得到null)。您的主要方法如下所示,可以正常工作。我还修复了Antoine提到的错误
public static void Main()
{
string filePath = "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml";
WebClient client = new WebClient();
string reply = client.DownloadString(filePath);
var input = new StringReader(reply);
//var yamlStream = new YamlStream();
//yamlStream.Load(input);
Deserializer deserializer = new Deserializer();
//var icons = deserializer.Deserialize<IconSearch>(input);
//Testing my own implementation
//if (icons == null)
// Console.WriteLine("Icons is null");
//Testing Shekhar's suggestion
var root = deserializer.Deserialize<Rootobject>(input);
if (root == null)
Console.WriteLine("Root is null");
}
publicstaticvoidmain()
{
字符串文件路径=”https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/src/icons.yml";
WebClient客户端=新的WebClient();
string reply=client.DownloadString(文件路径);
var输入=新的StringReader(回复);
//var yamlStream=新的yamlStream();
//yamlStream.Load(输入);
反序列化程序反序列化程序=新的反序列化程序();
//变量图标=反序列化器。反序列化(输入);
//测试我自己的实现
//如果(图标==null)
//Console.WriteLine(“图标为空”);
//测试谢哈尔的建议
var root=反序列化程序。反序列化(输入);
if(root==null)
Console.WriteLine(“根为空”);
}
谢谢,但这似乎也不起作用。我在这里做了一个.NET摆弄:我的实现之所以能够工作是有道理的,因为属性名称不正确,但我确实假设我不需要yaml中的所有属性。有没有一种方法可以像JSON.NET那样反序列化到不同的属性名?有人能拿出一把能用的小提琴吗?你的小提琴里有一只虫子。您在if(icons==null)控制台中测试了错误的变量代码>。我在这里做到了:关于外壳,您可能想使用CamelCaseNamingConvention
,或者用yalmemberattribute
@AntoineAubry装饰每个成员。我知道这不是理想的命名约定,但奇怪的是,它是由Visual Studio生成的。我想他们不想规定你们应该使用什么命名约定。谢谢你们,伙计们,我知道我现在错在哪里了。由于我更喜欢将数据放在更通用的对象列表中(如IconSearch),因此可以使用其他排版搜索实现,所以我使用automapper将两个列表映射到一起。