You'll want to use XML serialization. Take a look at this MSDN article. Here's an excerpt on serialization and deserialization:
How to Serialize an Object
To Serialize and object, we need few
instances of the in-built classes. So
lets first create an instance of a
XmlDocument class from System.Xml
namespace. Then create an instance of
XmlSerializer class from
System.Xml.Serialization namespace
with parameter as the object type. Now
just create an instance of the
MemoryStream class from System.IO
namespace that is going to help us to
hold the serialized data. So all your
instances are there, now you need to
call their methods and get your
serialzed object in the xml format. My
function to Serialize an object looks
like following.
private string SerializeAnObject(object obj)
{
System.Xml.XmlDocument doc = new XmlDocument();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
System.IO.MemoryStream stream = new System.IO.MemoryStream();
try
{
serializer.Serialize(stream, obj);
stream.Position = 0;
doc.Load(stream);
return doc.InnerXml;
}
catch
{
throw;
}
finally
{
stream.Close();
stream.Dispose();
}
}
How to DeSerialize an Object
To DeSerialize an object you need an
instance of StringReader, XmlReader
and XmlSerializer class in order to
read the xml data (Serialized data),
read it into XmlReader and DeSerialize
it respectively. So in brief my
function to DeSerialize the object
looks like following.
private object DeSerializeAnObject(string xmlOfAnObject)
{
MyClass myObject = new MyClass();
System.IO.StringReader read = new StringReader(xmlOfAnObject);
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(myObject.GetType());
System.Xml.XmlReader reader = new XmlTextReader(read);
try
{
myObject = (MyClass)serializer.Deserialize(reader);
return myObject;
}
catch
{
throw;
}
finally
{
reader.Close();
read.Close();
read.Dispose();
}
}