Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i load XML file value to listbox, when window is loaded ??.
I want like in XML file listbox item will be saved. after window is loaded previous xml value will be loaded to Listbox.

Please anyone help me to solve this problem..

What I have tried:

void Window_Loaded(object sender, RoutedEventArgs e)
{
XElement xml = XElement.Parse(stringJoin("", File.ReadAllLines("WH_Num.xml")));

foreach (XElement el in xml.Elements())
{
ListBoxItem item = new ListBoxItem();
string name = el.Attribute("name").Value;
string value = el.Attribute("value").value;
item.Content = name + ": " + value;

ListBox1.Items.Add(item);
}
}


I tried above code for Loading, but it was not working. After window is loaded listbox is still empty.
Posted
Updated 13-May-20 0:48am
v2
Comments
Richard MacCutchan 13-May-20 6:04am    
Use your debugger to trace the code and see exactly what values are being extracted from the XML source.

As well as Richard's suggestion, I'd try simplifying the 'add loop' to

foreach (XElement el in xml.Elements())
{
    string listBoxElement = "";
    string name = el.Attribute("name").Value;
    string value = el.Attribute("value").value;
    listBoxElement  = name + ": " + value;

    ListBox1.Items.Add(listBoxElement);
}
 
Share this answer
 
Comments
Member 14672509 13-May-20 6:51am    
i tried now, but it also not working. after window loading listbox will be empty.
I would recommend using a XmlDataProvider and the ListBox ItemsSource.
See example here: wpf - Displaying data from an XML file, on a listbox - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900