linq to xml初始化数组
我有两个类 - Cd
和 Track
,我正在尝试使用 Linq 从 XML 文件中读取它们。
public Cd(字符串 t, 字符串 a, 字符串 cat, DateTime rls, Track[] tr)
public Track(string t, int l)
XML 如下所示。
<media> <cd> <artist>Ozzy Osbourne</artist> <album Name="Bark at the moon" Type="Metal" Tracks="5" ReleaseDate="1983-12-10"> <track Length="300">Bark at the moon</track> <track Length="235">You're No Different</track> <track Length="567">Now You See It (Now You Don't)</track> <track Length="356">Rock 'N' Roll Rebel</track> <track Length="120">Centre of Eternity</track> </album> </cd> <cd> <artist>Journey</artist> <album Name="Escape" Type="Rock" Tracks="4" ReleaseDate="1981-07-31"> <track Length="300">Don't Stop Believin'</track> <track Length="235">Stone in Love</track> <track Length="567">Who's Crying Now</track> <track Length="356">Keep on Runnin'</track> </album> </cd> </media>
我尝试使用的代码如下
XElement xdoc = XElement.Load("dbxml.xml"); var temp = from cds in xdoc.Descendants("cd") select new Cd( cds.Element("artist").Value, cds.Element("album").Attribute("Name").Value, cds.Element("album").Attribute("Type").Value, DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value), new Track[] { // One Track reads fine.. new Track(cds.Element("album").Element("track").Value, int.Parse(cds.Element("album").Element("track").Attribute("Length").Value)) } );
问题是我不知道如何使用从 XML 文件读取的所有曲目来初始化数组。我可以将整个查询包装在 .ToList()
中,使用匿名类型和 foreach
-it 但我想知道是否有办法做到这一点只需用 linq 运行一次即可。
cds.Elements("album").Elements("song")
返回一个 IEnumerable
集合,并且以某种方式应将其作为范围添加到数组中并转换转换成字符串和整数,或者其他有影响的东西。有什么帮助吗?
谢谢!
I have two classes - Cd
and Track
which I'm trying to read into from a XML file using Linq.
public Cd(string t, string a, string cat, DateTime rls, Track[] tr)
public Track(string t, int l)
The XML looks like this.
<media> <cd> <artist>Ozzy Osbourne</artist> <album Name="Bark at the moon" Type="Metal" Tracks="5" ReleaseDate="1983-12-10"> <track Length="300">Bark at the moon</track> <track Length="235">You're No Different</track> <track Length="567">Now You See It (Now You Don't)</track> <track Length="356">Rock 'N' Roll Rebel</track> <track Length="120">Centre of Eternity</track> </album> </cd> <cd> <artist>Journey</artist> <album Name="Escape" Type="Rock" Tracks="4" ReleaseDate="1981-07-31"> <track Length="300">Don't Stop Believin'</track> <track Length="235">Stone in Love</track> <track Length="567">Who's Crying Now</track> <track Length="356">Keep on Runnin'</track> </album> </cd> </media>
The code I'm trying to use is as follows
XElement xdoc = XElement.Load("dbxml.xml"); var temp = from cds in xdoc.Descendants("cd") select new Cd( cds.Element("artist").Value, cds.Element("album").Attribute("Name").Value, cds.Element("album").Attribute("Type").Value, DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value), new Track[] { // One Track reads fine.. new Track(cds.Element("album").Element("track").Value, int.Parse(cds.Element("album").Element("track").Attribute("Length").Value)) } );
The problem is that I don't know how to initialize the array with all the tracks read from the XML file. I could wrap the whole query in a .ToList()
, use an anonomyous type and foreach
-it but I'd like to know if there is a way to do this in just one run with linq.
cds.Elements("album").Elements("song")
returns an IEnumerable<XElement>
collection and somehow that should be added to the array as a range and turned into a string and an int, or something to that affect. Any help out there?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会起作用:
使用属性更容易阅读(imo):
您应该考虑向
Cd
和Track
添加默认构造函数并公开公共属性或使用命名参数 - 这将使LINQ 语句更具可读性。This would work:
Easier to read (imo) using properties:
You should consider adding a default constructor to
Cd
andTrack
and exposing public properties or use named parameters - this would make the LINQ statement much more readable.在您的 LINQ 中,替换
为如下内容:
In your LINQ, replace
with something like this: