title: Load XMLDocument from byte array (optionally containing BOM characters)
body: <p>I've seen several posts here on SO about loading XML documents from some data source where the data has Microsoft's proprietary UTF-8 preamble (for instance, <a href="https://stackoverflow.com/questions/2111586/parsing-xml-string-to-an-xml-document-fails-if-the-string-begins-with-xml">this one</a>).<br>
  However, I can't find an elegant (and working!) solution which does not involve striping out BOM characters manually.</p>
  
  <p>For instance, there is <a href="https://stackoverflow.com/questions/2111586/parsing-xml-string-to-an-xml-document-fails-if-the-string-begins-with-xml/2111632#2111632">this example</a>:</p>
  
  <pre><code>byte[] b = System.IO.File.ReadAllBytes("c:\\temp_file_containing_bom.txt");
  using (System.IO.MemoryStream oByteStream = new System.IO.MemoryStream(b)) {
      using (System.Xml.XmlTextReader oRD = new System.Xml.XmlTextReader(oByteStream)) {
          System.Xml.XmlDocument oDoc = new System.Xml.XmlDocument();
          oDoc.Load(oRD);
          Console.WriteLine(oDoc.OuterXml);
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>...but it still keeps throwing "invalid data" exception.</p>
  
  <p>My problem is that I have a huge byte array which <em>sometimes</em> contains the BOM and sometimes it does not. I need to load it in XMLDocument. And I don't believe that I am the one who has to take care for the "helper" bytes.</p>