I'm new to c# and am trying to load some data in a xml file into different list boxes. I've tried some things, but nothing seems to work for me. I've managed to save data from the list boxes into a xml-file.
I tried to write some code trying to load the xml file into the list boxes :
private void OnLoad()
{
OpenFileDialog load = new OpenFileDialog();
//load.InitialDirectory = "c:\\";
load.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
load.FilterIndex = 2;
load.RestoreDirectory = true;
if (load.ShowDialog() == true)
{
using (StreamReader stream = new StreamReader(load.OpenFile()))
{
try
{
XmlDocument parsed = new XmlDocument();
parsed.Load(stream);
XmlNodeList foodList = parsed.GetElementsByTagName("Food");
for(int i = 0; i > foodList.Count; i++)
{
string var = elemList[i].Attributes["FoodName"].Value;
lb1.Items.Add(var);
}
catch(XmlException exception)
{
MessageBox.Show("The XML could not be read." + exception);
XmlDocument empty = new XmlDocument();
}
}
}
}
My XML files looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ListBox>
<lb1>
<Food FoodName="*****" />
</lb1>
<lb2>
<Variable FoodName="****"/>
</lb2>
</ListBox>
lb1 and lb2 is different list boxes. I want all the data in lb1 to be put into my list box with the same name, and all the data in lb2 to be put into my list box with the same name etc..