Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello these are my serialization and deserialization methods in c#. When I deserialize an object or something I get Error.

C#
public byte[] Serialize<T>(T ob)
        {
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(ob.GetType());
            MemoryStream ms = new MemoryStream();
            xs.Serialize(ms, ob);
            string xmlData = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, ms.ToArray().Length);
            return ms.ToArray();
        }

public T XMLDeSerialize<T>(byte[] data)
        {
            string xmlData = System.Text.Encoding.ASCII.GetString(data,0,data.Length);
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ms.Write(data, 0, data.Length);
            ms.Position = 0;
            XmlReader rdr = XmlReader.Create(ms);
            ms.Position = 0;
            T t = (T)xs.Deserialize(rdr);
            return t;
        }


What I have tried:

I tried the methods above and could not managed to work. I cant find any solution.
Posted
Updated 10-Aug-16 9:58am
v3
Comments
Kschuler 10-Aug-16 15:52pm    
What error do you get?
Eren Yatkin 11-Aug-16 2:50am    
I was getting "unexpected XML Error (0,0) ....but now these solutions working very well
Kschuler 11-Aug-16 13:45pm    
Glad someone helped you. Just remember next time to include what error you got. It makes it a lot easier to narrow down the problem. :)

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:
C#
ms.Write(data, 0, data.Length);
ms.Seek(0L, SeekOrigin.Begin);


With that change in place, your methods work perfectly:
C#
public byte[] Serialize<T>(T ob)
{
    var xs = new System.Xml.Serialization.XmlSerializer(ob.GetType());
    var ms = new MemoryStream();
    xs.Serialize(ms, ob);
    // string xmlData = System.Text.Encoding.ASCII.GetString(ms.ToArray(), 0, ms.ToArray().Length);
    return ms.ToArray();
}
 
public T DeSerialize<T>(byte[] data)
{
    // string xmlData = System.Text.Encoding.ASCII.GetString(data,0,data.Length);
    var xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
    var ms = new MemoryStream();
    ms.Write(data,0,data.Length);
    ms.Seek(0L, SeekOrigin.Begin); // <-- Add this line
    T t = (T)xs.Deserialize(ms);
    return t;
}
 
Share this answer
 
Comments
Eren Yatkin 10-Aug-16 15:57pm    
Thank you for your answer, well I found that I added ms.Position = 0; twice. When I deleted it it just work fine.
I found the solution.

I just removed
C#
ms.Position = 0
before
C#
T t = (T)xs.Deserialize(rdr);
İt worked like a charm.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900