我有这个的LINQ to XML请求其通过艺术家ID检索所有歌曲:使用请求与LINQ到XML
var query = from c in loaded.Descendants("artist")
where c.Attribute("Id").Value.Equals("1")
from s in c.Descendants("song")
select s.Attribute("path").Value.ToList();
当我尝试添加的结果在我的列表框这样的:
foreach (string track in query)
{
myList.Items.Add(track);
}
我得到这个错误:
错误CS0030:无法将类型 'System.Collections.Generic.List' 到 '字符串'
如何在我的ListBox中正确添加我的结果?谢谢你的帮助。
谢谢你的工作很好! – 2012-04-08 21:04:19
另外请注意,使用's.Attribute(“path”).value'可能有点危险,除非你完全确定属性'path'永远不会在xml中丢失。如果你不确定使用'(string)s.Attribute(“path”)',它将返回string.Empty,如果该属性在xml中缺失。 [请参阅MSDN的进一步解释](http://msdn.microsoft.com/en-us/library/system.xml.linq.xattribute.value.aspx) – Hauge 2012-04-08 21:13:27