title: How can I XML serialise to a memory stream and get the same results as if I'd serialised to a file stream?
body: <p>I am working on an application that stores its documents in XML using the C# serialisation / deserialisation.</p>
  
  <p>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.)</p>
  
  <p>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.</p>
  
  <p>My problem is that the files are identical apart from the XML header.  The file on disk begins with</p>
  
  <blockquote>
  <pre><code>&lt;?xml version="1.0"?&gt;
  </code></pre>
  </blockquote>
  
  <p>whereas the MemoryStream (when written to a file on disk) begins with </p>
  
  <blockquote>
  <pre><code>"&lt;?xml version="1.0" encoding="utf-8"?&gt;"
  </code></pre>
  </blockquote>
  
  <p>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?</p>
  
  <p>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.)</p>
  
  <p>The code that is serialising to file is:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Serialises the workspace.
      /// &lt;/summary&gt;
      /// &lt;param name="stream"&gt;
      /// The stream to serialise to.
      /// &lt;/param&gt;
      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();
      }
  </code></pre>
  
  <p>The code that is serialising to memory is:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Checks whether an object has changed since last save.
      /// &lt;/summary&gt;
      /// &lt;param name="storagePath"&gt;The path of the object.&lt;/param&gt;
      /// &lt;param name="current"&gt;The current memory version of the object.&lt;/param&gt;
      /// &lt;returns&gt;Whether the object has changed since last save.&lt;/returns&gt;
      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);
  </code></pre>
  
  <p>Thanks for any help,<br>
  Luke.</p>