How can I clone MemoryStream object?

15,124

Solution 1

I have solved my problem! :)
The things which were missing are marked with //*. Here is the code as it should be:

var xmlReaderSettings = new XmlReaderSettings();

stream.Position = 0;//*
xmlReaderSettings.IgnoreWhitespace = true;
using (var newMemoryStream = new MemoryStream())
{
    stream.CopyTo(newMemoryStream);
    newMemoryStream.Position = 0;  //*
    using (var newReader = XmlReader.Create(newMemoryStream, xmlReaderSettings))
    {
        newReader.MoveToContent(); //*

        Doing some stuff...
    }
}

Solution 2

Make sure to reset the position on newMemoryStream like so:

newMemoryStream.Position = 0;

after stream.CopyTo(newMemoryStream); but before creating the XmlReader

So the whole thing should look like this:

var x = new XmlReaderSettings();
x.IgnoreWhitespace = true;  

using (var newMemoryStream = new MemoryStream())
{
    stream.CopyTo(newMemoryStream);
    newMemoryStream.Position = 0;

    using (var newReader = XmlReader.Create(newMemoryStream,x)) //*
    {
        Doing some stuff...
    }
}

Also, since you're using another reader on the original stream prior to entering this method, make sure that the Position of the source stream is really where you want it to be.

Solution 3

You can use the following extension method to clone a MemoryStream, including its current position, and ensure the position of the original MemoryStream is reset to its original position after cloning:

public static MemoryStream Clone(this MemoryStream ms) {
    var pos = ms.Position;
    var ms2 = new MemoryStream();
    ms.CopyTo(ms2);
    ms.Position = pos;
    ms2.Position = pos;
    return ms2;
}
Share:
15,124
JavaSa
Author by

JavaSa

Updated on July 18, 2022

Comments

  • JavaSa
    JavaSa almost 2 years

    I have a MemoryStream object which is passed by Stream type parameter
    (Stream is abstract class in C#).

    I want to clone him and to create another MemoryStream object a side with current position of the original and to create also a new XMLReader out of it, so I will be able to read its content.

    This is what I did, and it's not working (debugging the line marked with //* -> newReader has got {None} value)
    Assumption: you are inside a method and have Stream currentStream reference.

    var x = new XmlReaderSettings();
    x.IgnoreWhitespace = true;  
    
     using (var newMemoryStream = new MemoryStream())
     {
             stream.CopyTo(newMemoryStream);
    
             using (var newReader = XmlReader.Create(newMemoryStream,x)) //*
             {
    
               Doing some stuff...
    
             }
     }
    
  • JavaSa
    JavaSa over 11 years
    Can't I copy from stream current position to newMemoryStream, I think I need that option
  • mlorbetske
    mlorbetske over 11 years
    @JavaSa Copy starts at the current position of the stream CopyTo is called on, but (as you said in your reply on your question) the position of newMemoryStream is 0 after the copy. After the copy, the target stream is at its end (or at least in the position the copy finished at) - in this case indicating that nothing was copied into it, probably because the source stream was at its end (or was itself empty).
  • JavaSa
    JavaSa over 11 years
    what you have said is correct but two other instructions were missing , see my answer.