#region " XML Serialization "
///
/// serialize an object to XML
///
/// the type of the input object
/// the object to serialize
///
public static string XmlSerialize(T objectToSerialize)
{
var ms = new MemoryStream();
var xs = new XmlSerializer(objectToSerialize.GetType());
var textWriter = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(textWriter, objectToSerialize);
ms = (MemoryStream)textWriter.BaseStream;
var xmlString = UTF8Encoding.UTF8.GetString(ms.ToArray());
// HACK : do this PI scrubbing more elegantly ...
if (xmlString.StartsWith("?"))
xmlString = xmlString.Replace("?", string.Empty);
return xmlString;
}
///
/// deserialize an object from xml
///
/// the type definition for the return object
/// the serialized object xml
/// the deserialized object
public static T XmlDeserialize(string xmlString)
{
var xs = new XmlSerializer(typeof(T));
var ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(xmlString));
var textWriter = new XmlTextWriter(ms, Encoding.UTF8);
var data = (T)xs.Deserialize(ms);
return data;
}
#endregion