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

.net c# entity-framework serialization


Below are two techniques I'm employing to serialize and deserialize entity framework objects (ver. 4.0). I made multiple attempts to do this but was unsuccessful. Serialization is effective. I get a well formed XML string, but when I attempt to deserialize, I see an XML problem. How is it even doable?


    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 serialize and deserialize data using the following methods:

/// <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 go backsb.ToString() when they load into theXmlDocument is not acting in any way.

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