Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to serialize and deserialize entity object in c#

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?

Thanks.

    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();
        doc.LoadXml(sb.ToString());
        string xml = doc.InnerXml;
        return xml;
    }
    public static object DeSerializeAnObject(string xml, Type objType)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
        XmlSerializer ser = new XmlSerializer(objType);
        object obj = ser.Deserialize(reader);
        return obj;
    }
like image 712
bobetko Avatar asked Jun 30 '11 21:06

bobetko


People also ask

What is serialize and deserialize in C?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What does it mean to serialize and deserialize data?

Data serialization is the process of converting an object into a stream of bytes to more easily save or transmit it. The reverse process—constructing a data structure or object from a series of bytes—is deserialization.

What is XML serialization and deserialization?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.


1 Answers

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); }
like image 197
IAbstract Avatar answered Sep 27 '22 22:09

IAbstract