Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:54:14+00:00 2026-05-25T13:54:14+00:00

I am trying to Serialize an object to XML however my object is a

  • 0

I am trying to Serialize an object to XML however my object is a generic list containing many records and causes the serializer to consume lots of memory. So I tried to serialize directly to a GZipStream with the following code:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)

        _ms.Position = 0
        formatter.Serialize(gzStream, obj)
        _ms.Position = 0
        gzStream.Flush()
        gzStream.Close()
    End Using

    _ms.Position = 0
    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1) 'this number doesn't matter, the data in my test sample is small
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

However I run into an InvalidDataException The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. when trying to read the data into the decompressData array.

When I Serialize to a separate memory stream first and then compress that stream such as:

Dim formatter As XmlSerializer = XmlSerializerFactory.GetSerializerForType(_type)
Using _ms As New MemoryStream()
    Dim uc_fileBytes() As Byte
    Dim uc_len As Integer
    Using _ms101 As New MemoryStream()
        formatter.Serialize(_ms101, obj)
        uc_fileBytes = _ms101.GetBuffer()
        uc_len = _ms101.Length
    End Using
    Using gzStream As New GZipStream(_ms, CompressionMode.Compress, True)
        _ms.Position = 0
        gzStream.Write(uc_fileBytes, 0, uc_len)
        gzStream.Flush()
        gzStream.Close()
    End Using

    Dim decompressData() As Byte
    Using gzStream As New GZipStream(_ms, CompressionMode.Decompress)
        ReDim decompressData(9000 - 1)
        Dim Len As Integer = gzStream.Read(decompressData, 0, decompressData.Length)
    End Using
End Using

It works fine without error. But why does it fail when I serialize directly to the GZipStream?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:54:14+00:00Added an answer on May 25, 2026 at 1:54 pm

    The cause of the problem is because the GZipStream behaves differently (obviously) to the MemoryStream when writing to it. It doesn’t handle paged writes very well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to serialize my object to xml. A serializer seemingly serializes all
I'm trying to serialize an object to XML that has a number of properties,
I've run into some problems when trying to serialize my object to XML. The
I have an XML file that I'm trying to serialize into an object. Some
im trying to serialize some object as xml and then read it back the
I am trying a write a program to serialize a object to a xml
i am trying to XML serialize a class object but having the following problem:
I'm getting this error while trying to de-serialize my object : [org.xml.sax.SAXParseException: cvc-complex-type.2.4.b: The
I am trying to serialize a .NET TimeSpan object to XML and it is
I'm trying to serialize an object to memory, pass it to another process as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.