title: What is the best way to load XML from a byte array or string with a document specification, as obtained from an OpenDocument ODT file?
body: <p>(NB: the original question title was: What is the best way to load XML from a string with a document specification?)</p>
  
  <p>I need to get the XML content from an ODT opendocument (LibreOffice) file in an XmlDocument object. The ODT is a zip archive and I managed to get the content.xml part as a byte array. Converting to a string seems simple, but I was surprised to find that XmlDocument.LoadXml(string) does not accept a string that starts with an Xml document specification line, like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Offices id="0" enabled="false"&gt;
    &lt;office /&gt;
  &lt;/Offices&gt;
  </code></pre>
  
  <p>The exception is: <code>Data at the root level is invalid. Line 1, position 1</code></p>
  
  <p>I wonder if there is a library call to read such a string?</p>
  
  <p>For now I use this function I improvised, but it feels unnecessarily complex to have to do stuff on the character level when handling xml documents:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Convert an Xml document in a string, including document specification line(s),
      /// to an XmlDocument object
      /// &lt;/summary&gt;
      /// &lt;param name="XmlString"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static XmlDocument LoadXmlString(string XmlString)
      {
          XmlDocument XmlDoc = new XmlDocument();
          XmlDoc.LoadXml(XmlString.Substring(XmlString.LastIndexOf("?&gt;") + 2));
          return XmlDoc;
      }
  </code></pre>
  
  <p>Is there a better way?</p>
  
  <p>NB: I refer to <a href="https://stackoverflow.com/questions/5748668/data-at-the-root-level-is-invalid">this earlier question</a></p>
  
  <p>but this addresses the problem of parsing a string, with the solution of converting the string to a byte array, while I should not be parsing the string, and not convert the byte array to string to begin with, but just skip this step and directly parse the byte array after unzipping the ODT.</p>