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 “Root 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)