Memory stream is empty
I need to generate a huge xml file from different sources (functions). I decide to use XmlTextWriter since it uses less memory than XmlDocument . First, initiate an XmlWriter with underlying MemoryStream MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false, false)); xmlWriter.Formatting = Formatting.Indented; Then I pass the XmlWriter (note xml writer is kept open until the very end) to a function to generate the beginning of the XML file: xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement(); // xmlWriter.WriteEndElement(); // Do