title: LINQ to XML file not saving correctly
body: <p>I have a method that removes sync times older than 10 sec's old and then adds a new one when fired:</p>
  
  <pre><code>    public void addSyncTime()
      {
          //delete archived sync times
          XMLDocObject.Descendants("LastSync")
             .Where(e =&gt; Convert.ToDateTime(e.Element("Time").Value) &lt; DateTime.Now.AddSeconds(-10))
             .Where(e =&gt; e.Element("id").Value != "1")
             .Remove();
          string t1 = XMLDocObject.ToString();
          //get the last primary key added
          var lastPrimaryKey = (from i in XMLDocObject.Element("DataLoadTimes").Elements("LastSync").Elements("id")
                                select i).LastOrDefault().Value;
          //increment it once
          int newPrimaryKey = Convert.ToInt32(lastPrimaryKey) + 1;
          //create element to insert
          var newElement = new XElement("LastSync",
                 new XElement("id", newPrimaryKey),
                 new XElement("Time", DateTime.Now.ToString()));
          //add to XML doc object
          XMLDocObject.Element("DataLoadTimes").Add(newElement);
          string t2 = XMLDocObject.ToString();
      }
  </code></pre>
  
  <p>The above strings are for breakpoint testing.</p>
  
  <p>This above code creates XML like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;!--QTabs Data Storage--&gt;
  &lt;DataLoadTimes&gt;
    &lt;LastSync&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:29:13 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
    &lt;LastSync&gt;
      &lt;id&gt;2&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:29:14 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
    &lt;LastSync&gt;
      &lt;id&gt;3&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:29:15 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
  &lt;/DataLoadTimes&gt;
  </code></pre>
  
  <p>Here is the save method to save to XML after the above is fired:</p>
  
  <pre><code>    public async void saveToXmlFile()
      {
          //storageFile = await storageFolder.GetFileAsync(Settings.xmlFile);
          using (Stream fileStream = await storageFile.OpenStreamForWriteAsync())
          {
              XMLDocObject.Save(fileStream);
          }
      }
  </code></pre>
  
  <p>Now, when using 1st method a few times and saving I am getting a corrupted XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;!--QTabs Data Storage--&gt;
  &lt;DataLoadTimes&gt;
    &lt;LastSync&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:23:53 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
    &lt;LastSync&gt;
      &lt;id&gt;2&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:27:04 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
  &lt;/DataLoadTimes&gt;  &lt;id&gt;3&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:26:43 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
    &lt;LastSync&gt;
      &lt;id&gt;4&lt;/id&gt;
      &lt;Time&gt;4/1/2015 10:26:44 AM&lt;/Time&gt;
    &lt;/LastSync&gt;
  &lt;/DataLoadTimes&gt;
  </code></pre>
  
  <p>Note this line: <code>&lt;/DataLoadTimes&gt;  &lt;id&gt;3&lt;/id&gt;</code></p>
  
  <p>So what's going on here? Debugging shows that the string attempting to be saved stored in the XDocucument object is right:</p>
  
  <pre><code>"&lt;!--QTabs Data Storage--&gt;\r\n&lt;DataLoadTimes&gt;\r\n  &lt;LastSync&gt;\r\n    &lt;id&gt;1&lt;/id&gt;\r\n    &lt;Time&gt;4/1/2015 10:23:53 AM&lt;/Time&gt;\r\n  &lt;/LastSync&gt;\r\n  &lt;LastSync&gt;\r\n    &lt;id&gt;2&lt;/id&gt;\r\n    &lt;Time&gt;4/1/2015 10:27:04 AM&lt;/Time&gt;\r\n  &lt;/LastSync&gt;\r\n&lt;/DataLoadTimes&gt;"
  </code></pre>
  
  <p>Why is the file getting messed up?</p>