简体   繁体   中英

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?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM