Firstly, get rid of the two
xmlData
lines. You're not using the variable, so there's no need for it.
Secondly, after writing the data to the
MemoryStream
, you need to move the "current position" back to the start of the stream:
ms.Write(data, 0, data.Length);
ms.Seek(0L, SeekOrigin.Begin);
With that change in place, your methods work perfectly:
public byte[] Serialize<T>(T ob)
{
var xs = new System.Xml.Serialization.XmlSerializer(ob.GetType());
var ms = new MemoryStream();
xs.Serialize(ms, ob);
return ms.ToArray();
}
public T DeSerialize<T>(byte[] data)
{
var xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
var ms = new MemoryStream();
ms.Write(data,0,data.Length);
ms.Seek(0L, SeekOrigin.Begin);
T t = (T)xs.Deserialize(ms);
return t;
}