c# How to load a memory stream to an xmldocument without BOM? (My memory stream is generated from XMLTextWriter without BOM)
<p>My problem is really strange.I need to write a huge xml file from different sources in c#.</p>

<p>So first to begin with,I use XmlTextWriter to write to a memory stream:</p>

<pre><code>     MemoryStream ms=new MemoryStream();
     XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        **settings.Encoding = new UTF8Encoding(false,false); // set UTF8 without BOM !!**
        XmlWriter xmlWriter = XmlWriter.Create(ms, settings);
        xmlWriter.WriteStartDocument();

        xmlWriter.WriteStartElement();

        xmlWriter.WriteEndElement(); 
        xmlWriter.WriteEndDocument()

        xmlWriter.Flush();

        xmlWriter.Close();
</code></pre>

<p>Note that ms will not be closed after closing xmlTextWriter. So I pass ms to a xmlDocument and write other parts of xml elements:</p>

<pre><code>        XmlDocument doc = new XmlDocument();
        ms.Position=0;
        doc.Load(ms);
</code></pre>

<p>Then I write by using:</p>

<pre><code>       XmlElement elmRoot=doc.DocumentElement(); //root
       XmlElement elm=doc.CreateElement("New Element");
       XmlAttribute att=doc.CreateAttribute("att");
       att.Value="ABC";
       elm.Attributes.Append(att);
       elmRoot.AppendChild(elm);
       ms.Position=0;
       doc.Save(ms)
</code></pre>

<p>However, after the above operations, I found that ms still has three BOM at the beginning of generated xml file, causing the file cannot be opened in a browser. Any ideas?</p>

<p>Thanks a lot!</p>
