Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a listbox, and i want to save the items into an xml file.
this is the code to save:

try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(string));
                TextWriter tw = new StreamWriter("C:\\Users\\Lifebook\\Desktop\\Usage Profiles.txt");
                for (int i = 0; i <= lstUsageProfile.Items.Count - 1; i++)
                {
                    serializer.Serialize(tw, lstUsageProfile.Items[i].ToString());
                }
                tw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving");
            }

This is the output of the xml

<string><node1></string>



this line is repeated for every node in the list box.

to save them it's no problem, but then i want to load them (for the next user who is working on the program)

but i can't load them up and put them back in the list box and removing the xml tags created by default.
pls help.



after that link, i improved my code to:

XmlSerializer deserializer = new XmlSerializer(typeof(List<string>));
TextReader textReader = new StreamReader("C:\\Users\\Lifebook\\Desktop\\Usage Profiles.txt");
List<string> up;
up = (List<string>)deserializer.Deserialize(textReader);
textReader.Close();

lstUsageProfile.Items.Add(up);</string></string></string>



and i'm getting this error

There is an error in XML document (2, 2).
Posted
Updated 25-Jul-11 2:31am
v6

The List was a change in the right direction. Take a look at this code for export and Import:

private void ExportXML()
{
   try
   {
      System.Collections.Generic.List<string> theList = new System.Collections.Generic.List<string>();
      XmlSerializer serializer = new XmlSerializer(typeof(System.Collections.Generic.List<string>));
      TextWriter tw = new StreamWriter(@"C:\Users\Lifebook\Desktop\Usage Profiles.txt");

      for (int i = 0; i <= lstUsageProfile.Items.Count - 1; i++)
      {
         theList.Add(lstUsageProfile.Items[i].ToString());
      }

      serializer.Serialize(tw, theList);
      tw.Close();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message.ToString());
   }
}


private void ReadXML()
{
   try
   {
      System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
      doc.Load(@"C:\Users\Lifebook\Desktop\Usage Profiles.txt");
      System.Xml.XmlElement root = doc.DocumentElement;
      System.Xml.XmlNodeList lst = root.GetElementsByTagName("string");

      foreach (System.Xml.XmlNode n in lst)
      {
         lstUsageProfile.Items.Add(n.InnerText);
      }
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message.ToString());
   }
}
 
Share this answer
 
v2
Comments
adnama 25-Jul-11 8:49am    
after your code this error came up :
Unexpected XML declaration. The XML declaration must be the first node in the document and no white space characters are allowed to appear before it. Line2,position 29.
unforgiven.1791 23-Aug-13 13:19pm    
Thanks . This code work for me .
UJimbo 25-Jul-11 9:07am    
Check your Usage Profiles.txt file. Try creating a new file instead, like "C:\Users\Lifebook\Desktop\example.xml" and try reading from that instead.
 
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