Xml loading from memory stream issue
Here is my code:
MemoryStream xmlStream = new MemoryStream();
XmlDocument xmlDoc = new XmlDocument();
XmlWriter xmlWriter = XmlWriter.Create(xmlStream);
//Add some elements and attributes.
xmlWriter.WriterEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
Ok, now that I've closed the XmlWriter is there any way to access the XmlStream again?
If I don't close then when I want to use xmlDoc.Load(xmlStream) it gives an exception that says "Ro开发者_StackOverflow社区ot Element is missing"
If you don't close the stream you can set the Position property to 0 to go back to the start and then create an XmlReader to read the stream back or use XmlDocument.Load as you're trying to do.
To summarise, remove xmlWriter.Close() and then call xmlStream.Position = 0, then call xmlDoc.Load(xmlStream)
Not as a stream - but you can get at the data.
MemoryStream.ToArray
works even after you've closed it.
No, if you want to access the underlying stream further, you shouldn't close the XmlWriter since closing it actually means closing the underlying stream. You can dispose of the stream after you're done with it using:
xmlStream.Dispose();
No, you can't access disposed (closed) object (stream) anymore.
精彩评论