1

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..

1
  • 1
    Side note: You might want to check out XmlSerializer although, there is some special handling regarding lists. Looks easier than what you have though. Commented Jun 29, 2017 at 18:58

1 Answer 1

2

The issue is that you are serializing your VariableItem objects to XML but when loading back from the file, you are attempting to add an object of type XmlDocument:

 XmlDocument parsed = new XmlDocument(); 
 parsed.Load(stream);
 InputsMV.Items.Add(parsed); //Problem line

You need to deserialize the data contained in the parsed object into objects of type VariableItem, then add those objects to your list. Something like this (more or less, adjust code to what you are doing):

 XmlDocument parsed = new XmlDocument(); 
 parsed.Load(stream);
 IEnumerable<VariableItem> items = LoadFromXml(parsed);
 foreach(var item in items)
 {
      InputsMV.Items.Add(item); 
 }

I would also suggest separating the serialization concern into its own class (have a VariableItemXmlSerializer class or something like that).

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you! My program doesn't seem to find LoadFromXml tough so it won't work, it isn't part of the NET.Framework right? @Juan
@Anna: You need to write the LoadFromXml method yourself. Use XDocument and Linq to load the file and parse it to a list of VariableItems.
I've modified my code a bit (changed it in my question). My problem now is that I want each variable item to be put in the right list box. Right now every variable item in my XML document ends up in the same box. @PalleDue
@Anna: Yes of course. You wrote them all to the same file so you have to select the right nodes when loading them back.
Yeah I know. But how do I manage to select the right nodes? @Juan
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.