DataContractSerializer serializing List<T> getting error
Asked Answered
G

4

7

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;
   }
} 
Glum answered 9/2, 2011 at 22:55 Comment(0)
O
1

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>;
                }
            }
        }
    }
Olinger answered 10/2, 2011 at 19:25 Comment(1)
thanks, that worked after i got rid of the XmlDictionaryReaderGlum
K
5

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();
    }
}
Kingwood answered 9/2, 2011 at 23:3 Comment(1)
Thanks, but i am still getting this error: 'There was an error deserializing the object of type System.Collections.Generic.List`1[[A.B.C.DataValues, A.B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Unexpected end of file. Following elements are not closed: Time, DataValues, ArrayOfDataValues.' Please see updated codeGlum
H
2

Sorry to be late to this question.

The problem with the initial approach was simply that you weren't flushing (read: disposing) the XmlDictionaryWriter.

This should work (note the 2nd using clause):

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

Hope this helps someone.

Hamamatsu answered 18/4, 2012 at 19:39 Comment(0)
O
1

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>;
                }
            }
        }
    }
Olinger answered 10/2, 2011 at 19:25 Comment(1)
thanks, that worked after i got rid of the XmlDictionaryReaderGlum
T
0

I ran exactly into the same problem and I finally found the solution : the XmlDictionaryWriter needs to be disposed/closed before the Stream you are writing into is itself closed. I discovered that thanks to the thorough example found at http://www.albahari.com/nutshell/ch15.aspx whiche are more complete than the MSDN ones.

In your sample code, that would be :

            using (XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress))
            {
                serializer.WriteObject(w, values);
            }

On my own example, using the XmlDictionaryWriter instead of the plain and by default Xml writer only gave me a ~25% decrease in file size but a factor 3 when reading back the object.

Tandy answered 19/4, 2012 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.