2011-02-09 72 views
0

我有两个类 - CdTrack,我试图从一个XML文件使用Linq读入。LINQ to XML初始化数组

public Cd(string t, string a, string 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()中,使用anonomyous类型和foreach-但我想知道是否有一种方法可以在linq中进行一次运行。

cds.Elements("album").Elements("song")返回一个IEnumerable<XElement>集合,并以某种方式将其作为范围添加到数组中,并转换为一个字符串和一个int值,或者影响它的值。有什么帮助吗?

谢谢!使用属性

XElement xdoc = XElement.Load("test.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), 
       cds.Element("album").Descendants("track").Select(t => new Track(t.Value, int.Parse(t.Attribute("Length").Value))).ToArray() 
       ); 

更易于读取(IMO):

回答

1

这会工作

select new Cd() 
    { 
     Artist = cds.Element("artist").Value, 
     Album = cds.Element("album").Attribute("Name").Value, 
     Type = cds.Element("album").Attribute("Type").Value, 
     ReleaseDate = DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value), 
     Tracks = cds.Element("album") 
        .Descendants("track") 
        .Select(t => new Track() 
        { 
         Name = t.Value, 
         Length = int.Parse(t.Attribute("Length").Value) 
        }).ToArray() 
    }; 

您应该考虑在CdTrack中添加默认构造函数,并公开属性或使用命名参数 - 这会使LINQ语句更具可读性。

0

在你的LINQ,更换

new Track[] { // One Track reads fine.. 
    new Track(cds.Element("album").Element("track").Value, 
     int.Parse(cds.Element("album").Element("track").Attribute("Length").Value)) 
     } 

像这样的东西:

(from t in cds.Element("album").Elements("track") 
    select new Track(..., ...) 
).ToArray() 
+0

谢谢!虽然我得到了所有剩余曲目重复的每张专辑的第一首曲目。五个月亮和四个树皮不要停止信仰'。也许我搞砸了吗? – citizencane 2011-02-09 20:54:33

+0

我错了元素和IIRC它是你想要的元素。 BrokenGlass的答案是使用.Descendants,这可能是更好的方法。 – ongle 2011-02-09 21:19:08