Using the XmlSerializer like you do will always save only one instance of the object type.
If you want to save more than one "Information" object, you can use the XmlSerializer on a
List<Information>
1) unserialize the list.
2) append the new record in the list
3) serialize the new list.
It's not the most efficient way, but it should be the closest way of your existing code.
Here is an code sample:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
namespace XmlSer
{
public class Information
{
private string m_data;
public string data
{
get { return m_data; }
set { m_data = value; }
}
}
class Program
{
static void Main(string[] args)
{
try
{
Information info = new Information();
info.data = System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToShortTimeString();
AppendData(info, "data.xml");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
static void AppendData(Information obj, string filename)
{
XmlSerializer xmlser = new XmlSerializer(typeof(List<Information>));
List<Information> list = null;
try
{
using (Stream s = File.OpenRead(filename))
{
list = xmlser.Deserialize(s) as List<Information>;
}
}
catch
{
list = new List<Information>();
}
list.Add(obj);
using (Stream s = File.OpenWrite(filename))
{
xmlser.Serialize(s, list);
}
}
}
}