Reputation: 86075
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
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
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