Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML WPF list ListBox
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 25-Jul-11 2:57am
adnama424
Edited 25-Jul-11 3:31am
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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());
   }
}
 
  Permalink  
v2
Comments
adnama at 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 at 23-Aug-13 13:19pm
   
Thanks . This code work for me .
UJimbo at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
0 Marcin Kozub 310
1 OriginalGriff 248
2 Sergey Alexandrovich Kryukov 215
3 Richard MacCutchan 202
4 Praneet Nadkar 197
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 25 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100