LinkedIn

Friday, 2 May 2008

How to Serialize and Deserialize objects. Greate for WCF

This code rocks thanks to Raymond Skelly for stealing it from someone to give to me!

public static string SerialiseObject(object o)
{
StringWriter sw = new StringWriter();
try
{
XmlSerializer s = new XmlSerializer(o.GetType());
s.Serialize(sw, o);
}
catch (Exception e)
{
string x = e.Message;
}
return sw.ToString();
}

public static object DeserialiseObject(string ObjectString, Type ObjectType)
{
XmlSerializer s = new XmlSerializer(ObjectType);
return s.Deserialize(new StringReader(ObjectString));
}