title: error in XML document. Unexpected XML declaration. XML declaration must be the first node in the document
body: <blockquote>
    <p>There is an error in XML document (8, 20). Inner 1: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.</p>
  </blockquote>
  
  <p>OK, I understand this error.</p>
  
  <p>How I get it, however, is what perplexes me.</p>
  
  <p>I create the document with Microsoft's Serialize tool. Then, I turn around and attempt to read it back, again, using Microsoft's Deserialize tool.</p>
  
  <p>I am not in control of writing the XML file in the correct format - that I can see.</p>
  
  <p>Here is the single routine I use to read and write.</p>
  
  <pre><code>private string xmlPath = System.Web.Hosting.HostingEnvironment.MapPath(WebConfigurationManager.AppSettings["DATA_XML"]);
  private object objLock = new Object();
  public string ErrorMessage { get; set; }
  
  public StoredMsgs Operation(string from, string message, FileAccess access) {
      StoredMsgs list = null;
      lock (objLock) {
          ErrorMessage = null;
          try {
              if (!File.Exists(xmlPath)) {
                  var root = new XmlRootAttribute(rootName);
                  var serializer = new XmlSerializer(typeof(StoredMsgs), root);
                  if (String.IsNullOrEmpty(message)) {
                      from = "Code Window";
                      message = "Created File";
                  }
                  var item = new StoredMsg() {
                      From = from,
                      Date = DateTime.Now.ToString("s"),
                      Message = message
                  };
                  using (var stream = File.Create(xmlPath)) {
                      list = new StoredMsgs();
                      list.Add(item);
                      serializer.Serialize(stream, list);
                  }
              } else {
                  var root = new XmlRootAttribute("MessageHistory");
                  var serializer = new XmlSerializer(typeof(StoredMsgs), root);
                  var item = new StoredMsg() {
                      From = from,
                      Date = DateTime.Now.ToString("s"),
                      Message = message
                  };
                  using (var stream = File.Open(xmlPath, FileMode.Open, FileAccess.ReadWrite)) {
                      list = (StoredMsgs)serializer.Deserialize(stream);
                      if ((access == FileAccess.ReadWrite) || (access == FileAccess.Write)) {
                          list.Add(item);
                          serializer.Serialize(stream, list);
                      }
                  }
              }
          } catch (Exception error) {
              var sb = new StringBuilder();
              int index = 0;
              sb.AppendLine(String.Format("Top Level Error: &lt;b&gt;{0}&lt;/b&gt;", error.Message));
              var err = error.InnerException;
              while (err != null) {
                  index++;
                  sb.AppendLine(String.Format("\tInner {0}: {1}", index, err.Message));
                  err = err.InnerException;
              }
              ErrorMessage = sb.ToString();
          }
      }
      return list;
  }
  </code></pre>
  
  <p>Is something wrong with my routine? If Microsoft write the file, it seems to me that it should be able to read it back.</p>
  
  <p>It should be generic enough for anyone to use.</p>
  
  <p>Here is my StoredMsg class:</p>
  
  <pre><code>[Serializable()]
  [XmlType("StoredMessage")]
  public class StoredMessage {
      public StoredMessage() {
      }
      [XmlElement("From")]
      public string From { get; set; }
      [XmlElement("Date")]
      public string Date { get; set; }
      [XmlElement("Message")]
      public string Message { get; set; }
  }
  
  [Serializable()]
  [XmlRoot("MessageHistory")]
  public class MessageHistory : List&lt;StoredMessage&gt; {
  }
  </code></pre>
  
  <p>The file it generates doesn't look to me like it has any issues.</p>
  
  <p><a href="https://i.stack.imgur.com/rTPiQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rTPiQ.jpg" alt="xml screenshot"></a></p>
  
  <p>I saw the solution here:</p>
  
  <p><a href="https://stackoverflow.com/q/11959304/153923">Error: The XML declaration must be the first node in the document</a></p>
  
  <p>But, in that case, it seems someone already had an XML document they wanted to read. They just had to fix it.</p>
  
  <p>I have an XML document created my Microsoft, so it should be read back in by Microsoft.</p>