This code (C# DotNet 1.0 onwards) works quite well to serialize most objects to XML. (and back)
It does not work for objects containing ArrayLists, and if possible stick to using only Arrays
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public static string SerializeToXMLString(object ObjectToSerialize)
{
MemoryStream mem = new MemoryStream();
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType());
ser.Serialize(mem,ObjectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(mem.ToArray());
}
Then to deserialze:
public static object DeSerializeFromXMLString(System.Type TypeToDeserialize, string xmlString)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
MemoryStream mem = new MemoryStream(bytes);
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(TypeToDeserialize);
return ser.Deserialize(mem);
}