user496949
user496949

Reputation: 86075

memorystream question

After write the xml document into the memory stream. When I want to use it by using XMLDocuments.Load, I have to set the position back to 0.

I am wondering If there any standard way to do it?

Upvotes: 1

Views: 1458

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500245

Well the simplest way is just:

stream.Position = 0;

I'm not sure what you're after beyond that. You can use the Seek method, but personally I find the Position property to be far simpler.

Do you definitely need to go via a stream in the first place? If you've already got the XmlDocument, why not just use that?

Upvotes: 2

cdhowie
cdhowie

Reputation: 168988

That's pretty much how you have to do it. The position must be set back to 0, because after writing the document into the stream, the stream is positioned at the end, ready to append more data. Setting the position to 0 effectively "rewinds" the stream, so that you will read it back in from the beginning.

This is a normal and expected usage pattern, if you are doing something like this anyway.

Upvotes: 2

Related Questions