In the following code, I serialize an object into an XML string.
But when I try to read this XML string into an XDocument
You can resolve your problem by using a StreamReader
to convert the data in the MemoryStream
to a string instead:
public static string SerializeObject(object o)
{
using (MemoryStream ms = new MemoryStream())
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (XmlWriter xtw = XmlWriter.Create(ms))
{
xs.Serialize(xtw, o);
xtw.Flush();
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
}
}
}