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 3439676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:20:20+00:00 2026-05-18T08:20:20+00:00

I am working on an application that stores its documents in XML using the

  • 0

I am working on an application that stores its documents in XML using the C# serialisation / deserialisation.

When the the user wants to perform a run in the application, we need to prompt the user to save the document if it has changed. (And also when the user tries to close the document.)

We evaluate whether the document has changed by comparing the object in memory to the document’s file on disk. The object in memory is XML serialised to a MemoryStream, and this is compared to a stream for the file on disk.

My problem is that the files are identical apart from the XML header. The file on disk begins with

<?xml version="1.0"?>

whereas the MemoryStream (when written to a file on disk) begins with

"<?xml version="1.0" encoding="utf-8"?>"

So of course, the two streams do not compare. What am I doing wrong? How can I get the results to be the same regardless of the stream I’m using?

Having looked into this issue some time ago, I got the impression that it might be to do with stream encoding formats, and that in C# the MemoryStream uses a different default encoding format to strings. (Or something.)

The code that is serialising to file is:

    /// <summary>
    /// Serialises the workspace.
    /// </summary>
    /// <param name="stream">
    /// The stream to serialise to.
    /// </param>
    private void Serialise(Stream stream)
    {
        stream.Seek(0, SeekOrigin.Begin);
        DeflateStream compressingStream = new DeflateStream(stream, CompressionMode.Compress, true);
        BufferedStream bufferedStream = new BufferedStream(compressingStream, 65536);
        new XmlSerializer(typeof(Workspace)).Serialize(bufferedStream, this);
        bufferedStream.Close();
        compressingStream.Close();
    }

The code that is serialising to memory is:

    /// <summary>
    /// Checks whether an object has changed since last save.
    /// </summary>
    /// <param name="storagePath">The path of the object.</param>
    /// <param name="current">The current memory version of the object.</param>
    /// <returns>Whether the object has changed since last save.</returns>
    private static bool HasObjectChanged(string storagePath, object current)
    {
        Stream streamCurr = new MemoryStream();
        DeflateStream compressingStream = new DeflateStream(streamCurr, CompressionMode.Compress, true);

...
        new XmlSerializer(current.GetType()).Serialize(compressingStream, current);

Thanks for any help,
Luke.

  • 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-18T08:20:21+00:00Added an answer on May 18, 2026 at 8:20 am

    Try using a StreamWriter:

    ...
    StreamWriter writer = new StreamWriter(bufferedStream);
    new XmlSerializer(typeof(Workspace)).Serialize(writer, this);
    writer.Close();
    ...
    

    And:

    ...
    StreamWriter writer = new StreamWriter(compressingStream);
    new XmlSerializer(current.GetType()).Serialize(writer, current);
    writer.Close();
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an iPhone Application that stores images in the Applications 'Document'
A database application that I'm currently working on, stores all sorts of settings in
We're working on an application that displays information through a Direct3D visualisation. A late
I am working on an application that installs a system wide keyboard hook. I
I am working on an application that is about 250,000 lines of code. I'm
I am working on an application that detects the most prominent rectangle in an
I'm working on an application that is implemented as an HTA. I have a
I'm working on an application that is supposed to create products (like shipping insurance
I'm working with an application that logs to Windows Application Log regularly (viewable through
I am working on an application that allows users to input Japanese language characters.

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.