DataContractSerializer serializing List<T> getting error

Kev84 picture Kev84 · Feb 9, 2011 · Viewed 11k times · Source

I am currently trying to serialize a List, it serializes (I think fine), but when it deserialize,

Sorry for the amount of code, but I am really stuck and have no idea why this is happening, i also tried to changed the struct into a class and no help.

THANKS.

i get the following error UPDATED

    There was an error deserializing the object of type There was an error deserializing the object of type 
`System.Collections.Generic.List`1[[A.B.C.DataValues, A.V, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Unexpected end of file. Following elements are not closed: Time, DataValues, ArrayOfDataValues.`

I am serializing like this UPDATED

     public void SerializeDataValue(List<DataValues> values)
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));

                using (MemoryStream stream = new MemoryStream())
                {
                    using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
                    {
                        XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress);
                        serializer.WriteObject(w, values);

                    }
                    _serializedData = stream.ToArray();
                }
            }

I am deserializing like this UPDATED

 public List<DataValues> DeserializeDataValue()
{
    if (SerializedData == null || SerializedData.Length == 0)
    {
        return new List<DataValues> ();
    }
    else
    {
        DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));
        using (MemoryStream stream = new MemoryStream(SerializedData))
        {
            using (GZipStream decompress = new GZipStream(stream, CompressionMode.Decompress))
            {
                XmlDictionaryReader r = XmlDictionaryReader.CreateBinaryReader(decompress, XmlDictionaryReaderQuotas.Max);
                return serializer.ReadObject(r, true) as List<DataValues>;
            }
        }
    }
}

Properties

private byte[] _serializedData;

[DataMember]
[Browsable(false)]
public byte[] SerializedData
{
    get { return _serializedData; }
    set { _serializedData = value; }
}

helper Methods

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    input.Position = 0;
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

Struct

[DataContract(Name = "DataValues", Namespace = "A.B.C")]
public struct DataValues
{
    [DataMember]
    public DateTime Time { get; set; }
    [DataMember]
    public Single Value { get; set; }

    public DataValues(DateTime dateTime, Single value)
    {
        Time = dateTime;
        Value = value;
   }
} 

Answer

Timwi picture Timwi · Feb 10, 2011

It’s because you are not serialising the object(s) completely. You need to close the stream(s) after writing, especially when using gzip. Recommended practice is to use using:

public void SerializeDataValue(List<DataValues> values)
{
    DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));
    using (MemoryStream stream = new MemoryStream())
    {
        using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
        {
            XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress);
            serializer.WriteObject(w, values);
        }
        _serializedData = stream.ToArray();
    }
}