When parsing XML with Linq, only one object is retrieved

I am trying to populate an array with the following xml:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
   <data>
         <item>
              <date>1307018090</date>
              <price>10.4718867</price>
              <low>10.38100000</low>
              <high>10.49000000</high>
              <nicedate>14:39</nicedate>
         </item>
         <item>
              ...
         </item>

etc.

I am using this Linq query, which for me means that it will create one object in:

var items = from item in doc.Element("data").Descendants()
                    select new Currency
                    {
                        Close = item.Element("price").Value.ToString(),
                        Date = item.Element("date").Value.ToString(),
                        Low = item.Element("low").Value.ToString(),
                        High = item.Element("high").Value.ToString(),
                        Time = item.Element("nicedate").Value.ToString()
                    };

And when I go through the elements, only one element is selected. I'm not very used to Linq, so I can’t figure out how to properly construct this statement. Any suggestions?

+3
source share
2 answers

You need to run Linq-Xml this way

var items = 
from item in 
doc.Element("data")
.Elements("item")
+6
source

Descedants() , , grand-grand-children .. , , LINQ, <item> <date> ( , , ).

Descedants() Elements("item"), @DaveShaw

+1

All Articles