【发布时间】:2011-02-09 20:21:24
【问题描述】:
我有两个类 - Cd 和 Track 我正在尝试使用 Linq 从 XML 文件中读取它们。
public Cd(string t, string a, string cat, DateTime rls, Track[] tr)public Track(string t, int l)
XML 看起来像这样。
我尝试使用的代码如下
XElement xdoc = XElement.Load("dbxml.xml"); var temp = 来自 xdoc.Descendants("cd") 中的 cds 选择新的镉( cds.Element("艺术家").Value, cds.Element("专辑").Attribute("Name").Value, cds.Element("专辑").Attribute("类型").Value, DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value), new Track[] { // 一个音轨读取良好.. 新曲目(cds.Element(“专辑”).Element(“曲目”).Value, int.Parse(cds.Element("album").Element("track").Attribute("Length").Value)) } );问题是我不知道如何使用从 XML 文件中读取的所有轨道初始化数组。我可以将整个查询包装在.ToList() 中,使用异常类型和foreach-it,但我想知道是否有一种方法可以在使用 linq 的一次运行中做到这一点。
cds.Elements("album").Elements("song") 返回一个IEnumerable<XElement> 集合,并且应该以某种方式将其作为范围添加到数组中并转换为字符串和int,或其他类似的东西。有什么帮助吗?
谢谢!
【问题讨论】:
标签: c# xml linq-to-xml initialization anonymous-types