In C#, I'm attempting to serialize and deserialize entity objects.

.net c# entity-framework serialization


I am using two methods below to serialize/deserialize entity framework object (ver. 4.0). I tried several ways to accomplish this, and had no luck. Serialization works fine. I get nice xml formatted string, but when I try to deserialize I get error in XML. How is that possible?


    public static string SerializeObject(Object obj)
        XmlSerializer ser = new XmlSerializer(obj.GetType());
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter writer = new System.IO.StringWriter(sb);
        ser.Serialize(writer, obj);
        XmlDocument doc = new XmlDocument();
        string xml = doc.InnerXml;
        return xml;
    public static object DeSerializeAnObject(string xml, Type objType)
        XmlDocument doc = new XmlDocument();
        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
        XmlSerializer ser = new XmlSerializer(objType);
        object obj = ser.Deserialize(reader);
        return obj;
6/30/2011 9:54:11 PM

Accepted Answer

I use generic methods to serialize and deserialize:

/// <summary>
/// Serializes an object to Xml as a string.
/// </summary>
/// <typeparam name="T">Datatype T.</typeparam>
/// <param name="ToSerialize">Object of type T to be serialized.</param>
/// <returns>Xml string of serialized type T object.</returns>
public static string SerializeToXmlString<T>(T ToSerialize)
    string xmlstream = String.Empty;

    using (MemoryStream memstream = new MemoryStream())
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        XmlTextWriter xmlWriter = new XmlTextWriter(memstream, Encoding.UTF8);

        xmlSerializer.Serialize(xmlWriter, ToSerialize);
        xmlstream = UTF8ByteArrayToString(((MemoryStream)xmlWriter.BaseStream).ToArray());

    return xmlstream;

/// <summary>
/// Deserializes Xml string of type T.
/// </summary>
/// <typeparam name="T">Datatype T.</typeparam>
/// <param name="XmlString">Input Xml string from which to read.</param>
/// <returns>Returns rehydrated object of type T.</returns>
public static T DeserializeXmlString<T>(string XmlString)
    T tempObject = default(T);

    using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(XmlString)))
        XmlSerializer xs = new XmlSerializer(typeof(T));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

        tempObject = (T)xs.Deserialize(memoryStream);

    return tempObject;

// Convert Array to String
public static String UTF8ByteArrayToString(Byte[] ArrBytes)
{ return new UTF8Encoding().GetString(ArrBytes); }
// Convert String to Array
public static Byte[] StringToUTF8ByteArray(String XmlString)
{ return new UTF8Encoding().GetBytes(XmlString); }
6/30/2011 10:04:25 PM

Popular Answer

Just return sb.ToString(), loading into the XmlDocument is not doing anything.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow