Linq 将system.collection.generic.list转换为list
我正在使用linq代码解析XML文件。这是我的代码。我想绑定详细信息和图像列表Linq 将system.collection.generic.list转换为list,linq,c#-4.0,xml-parsing,linq-to-xml,Linq,C# 4.0,Xml Parsing,Linq To Xml,我正在使用linq代码解析XML文件。这是我的代码。我想绑定详细信息和图像列表 XmlSerializer serializer = new XmlSerializer(typeof(Notchs)); XDocument xmlDoc = XDocument.Parse(dataInXmlFile); Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader()); var que
XmlSerializer serializer = new XmlSerializer(typeof(Notchs));
XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader());
var query = from l in xmlDoc.Descendants("Category")
select new Notch
{
name = (string)l.Attribute("name").Value,
Titles = l.Element("Articles").Elements("article")
.Select(s => s.Attribute("title").ToString())
.ToList(),
Image = l.Element("Articles").Elements("article").Elements("thumb_image").Elements("image")
.Select(x => x.Attribute("url").ToString()).ToList()
};
foreach (var result in query)
{
Console.WriteLine(result.name);
foreach (var detail in result.Titles)
{
Console.WriteLine(detail);
}
}
NotchsList.ItemsSource = query.ToList();
name
System.Collection.Generic.List'1[string.system]
name
System.Collection.Generic.List'1[string.system]
我尝试了这段代码,但我得到的输出如下..但我想详细信息和图像列表
XmlSerializer serializer = new XmlSerializer(typeof(Notchs));
XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
Notchs notchs = (Notchs)serializer.Deserialize(xmlDoc.CreateReader());
var query = from l in xmlDoc.Descendants("Category")
select new Notch
{
name = (string)l.Attribute("name").Value,
Titles = l.Element("Articles").Elements("article")
.Select(s => s.Attribute("title").ToString())
.ToList(),
Image = l.Element("Articles").Elements("article").Elements("thumb_image").Elements("image")
.Select(x => x.Attribute("url").ToString()).ToList()
};
foreach (var result in query)
{
Console.WriteLine(result.name);
foreach (var detail in result.Titles)
{
Console.WriteLine(detail);
}
}
NotchsList.ItemsSource = query.ToList();
name
System.Collection.Generic.List'1[string.system]
name
System.Collection.Generic.List'1[string.system]
我想你的
Titles = l.Element("Articles").Elements("article")
.Select(s => s.Attribute("title").ToString())
.ToList()
正在返回一个
IEnumerable
。您可能需要执行一个。SelectMany
而不是。Select
我尝试了这个方法,但出现了错误,例如无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”请提供一些示例数据(XML)以及您的输出应该是什么样子。@user123:以后请编辑您的问题,而不是发布新的问题。如果您不知道如何编辑,请阅读。