Reputation: 297
I have this xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Calendar>
<item id="34">
<Date>26 Apr</Date>
<Name>aa</Name>
<Date>26 Apr</Date>
<Name>aaa</Name>
<Date>23 Apr</Date>
<Name>aaaa</Name>
<Date>23 Apr</Date>
<Name>aaaaa</Name>
</item>
<item id="35">
<Date>27 Apr</Date>
<Name>aa</Name>
<Date>27 Apr</Date>
<Name>aaa</Name>
<Date>27 Apr</Date>
<Name>aaaa</Name>
<Date>27 Apr</Date>
<Name>aaaaa</Name>
</item>
</Calendar>
this is my class
public class Calendar
{
public string Name{ get; set; }
public string Data { get; set; }
}
listBox.ItemsSource =
from var in xml.Descendants("item")
orderby Convert.ToInt32(var.Attribute("id").Value) ascending
select new Calendar
{
Name= var.Element("Name").Value,
Data = var.Element("Data ").Value,
};
but in listBox i have only the first date and name of every item
Upvotes: 1
Views: 949
Reputation: 1459
class Program { static void Main(string[] args) {
XDocument xml =
XDocument.Load(
@"Path to your xml");
var q = from x in xml.Descendants("item")
orderby Convert.ToInt32(x.Attribute("id").Value) ascending
select new Calendar
{
Name = x.Elements("Name").Select(a => a.Value).ToList<String>(),
Date = x.Elements("Date").Select(a => a.Value).ToList<String>()
};
List<Calendar> calendars = q.ToList<Calendar>();
}
public class Calendar
{
public List<String> Name { get; set; }
public List<String> Date { get; set; }
}
}
Upvotes: 2
Reputation: 108937
XElement.Element(elementName)
only gets the first element that matches the elementName. Try a different approach using XElement.Elements(elementName)
Upvotes: 2