c# - linq to xml initializing array -


i have 2 classes - cd , track i'm trying read 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 this.

 <media>   <cd>     <artist>ozzy osbourne</artist>     <album name="bark @ moon" type="metal" tracks="5" releasedate="1983-12-10">       <track length="300">bark @ moon</track>       <track length="235">you're no different</track>       <track length="567">now see (now 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 use follows

 xelement xdoc = xelement.load("dbxml.xml");     var temp = 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[] { // 1 track reads fine..             new track(cds.element("album").element("track").value,                int.parse(cds.element("album").element("track").attribute("length").value))                 }         );  

the problem don't know how initialize array tracks read xml file. wrap whole query in .tolist(), use anonomyous type , foreach-it i'd know if there way in 1 run linq.

cds.elements("album").elements("song") returns ienumerable<xelement> collection , somehow should added array range , turned string , int, or affect. out there?

thanks!

this work:

xelement xdoc = xelement.load("test.xml"); var temp = 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()                  ); 

easier read (imo) using properties:

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()     }; 

you should consider adding default constructor cd , track , exposing public properties or use named parameters - make linq statement more readable.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -