0

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

Community
  • 1
  • 1
grizzo
  • 141
  • 1
  • 3
  • 10

1 Answers1

0

It's hard to tell without knowing how did you implement solution proposed in the linked question. Assuming you have a Users class to map the XML as suggested there :

public class Users
{
    [XmlElement("user")]
    public User[] UserList { get; set; }
}

You can show the data in ListBox this way for example :

XmlSerializer ser = new XmlSerializer(typeof(Users));
var u = (Users)ser.Deserialize(stream);
listBox.ItemsSource = u.UserList;

UPDATE :

According to new information you posted, you can try this :

XmlSerializer ser = new XmlSerializer(typeof(Contacts));
var u = (Contacts)ser.Deserialize(stream);
listBox_buildings.ItemsSource = u.BuildingList;
har07
  • 88,338
  • 12
  • 84
  • 137
  • Mm the compiler gives me: "'System.Windows.Controls.ListBox' does not contain a definition for 'ItemSource' and no extension method 'ItemSource' accepting a first argument of type 'System.Windows.Controls.ListBox' could be found (are you missing a using directive or an assembly reference?)" I'm adding new informations to my original post – grizzo Jul 22 '14 at 15:09
  • I've tried the exact same thing but it gives me the error that I have written above – grizzo Jul 23 '14 at 11:56