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