Search code examples
c#serializationdatacontractserializer

DataContractSerializer serializing List<T> getting error


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;
   }
} 

Solution

  • I can get the sample to work by removing the XmlDictionaryReader and instead directly feeding the input/output stream into the DataContractSerializer. It may be a defect in the XmlDictionaryReader for large compressed collections but I'm not sure.

    Hope this helps:

    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))
                        {
                            serializer.WriteObject(compress , values);
    
                        }
                        _serializedData = stream.ToArray();
                    }
                }
    
        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))
                    {
                        return serializer.ReadObject(decompress , true) as List<DataValues>;
                    }
                }
            }
        }