I've already did a similar thing with this simple code:
var data = from query in xml.Descendants("item")
select new Object
{
Element = (string)query.Element("element"),
};
listBox.ItemsSource = data;
But I had to deal also with a nested XML so I was worried that the code wouldn't have worked with that file, so I followed this topic Deserializing nested xml into C# objects and now I don't know how to show the datas in a listbox.
I use this class to serialize the XML:
{
[XmlRoot("contacts")]
public class Contacts
{
[XmlElement("building")]
public Building[] BuildingList { get; set; }
}
public class Building
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("address")]
public string Address { get; set; }
[XmlElement("tel")]
public string Tel { get; set; }
[XmlElement("email")]
public string Email { get; set; }
[XmlElement("link")]
public string Link { get; set; }
[XmlElement("timing")]
public string Timing { get; set; }
[XmlArray("contacts"), XmlArrayItem("contact")]
public Contact[] ContactList { get; set; }
}
[XmlRoot("contact")]
public class Contact
{
[XmlElement("surname")]
public string Surname { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("area")]
public string Area { get; set; }
[XmlElement("role")]
public string Role { get; set; }
[XmlElement("tel")]
public string Tel { get; set; }
[XmlElement("email")]
public string email { get; set; }
}
This is my listbox:
<ListBox x:Name="listBox_buildings">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top">
<TextBlock x:Name="text_title" Foreground="Black" FontSize="25" TextWrapping="Wrap" HorizontalAlignment="Stretch" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Anybody has some clues? Thanks