0

In my application I need serialize large object to xml string. But then serialized throw System.OutOfMemory exception. How can I serialized object without exception and with compression?

public static string GenerateXMLData<T>(T data)
    {
        byte[] bytes;
        using (var memoryStream = new MemoryStream())
        {
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(gZipStream, data);
            }
            bytes = memoryStream.ToArray();
        }

        return Encoding.UTF8.GetString(bytes);
    }
zrabzdn
  • 995
  • 2
  • 14
  • 33
  • 1
    [BinaryFormatter vs. Manual Serializing](http://www.codeproject.com/Articles/311944/BinaryFormatter-or-Manual-serializing) might be useful. – Yurii Apr 22 '14 at 07:50
  • Why do you have a BinaryFormatter.Serialize() and expect to get Xml ? You need XmlSerializer – joedotnot Mar 31 '19 at 03:49

1 Answers1

0

there is a lot of good answeers here on stackoverflow that can be accesed through a quick Google searche here is one i found

Serialize an object to XML

and another that i had to use some time back

How to make a serializable class that contains an instance of one class from a set of classes

hope it helps mate :)

Community
  • 1
  • 1