Extra characters in XML file after XDocument Save

When I had a similar problem in Python, I discovered that I was overwriting the beginning of the file without truncating it afterwards.

Looking at your code, I'd say you might be doing the same:

stream.Position = 0;
doc.Save(stream);
stream.Close();

Try setting the stream length to its post-save location as per this answer:

stream.Position = 0;
doc.Save(stream);
stream.SetLength(stream.Position);
stream.Close();

The most reliable way is to re-create it:

XDocument doc; // declare outside of the using scope
using (IsolatedStorageFileStream stream = isf.OpenFile("inventories.xml", 
           FileMode.Open, FileAccess.Read))
{
    doc = XDocument.Load(stream);
}

// change the document here

using (IsolatedStorageFileStream stream = isf.OpenFile("inventories.xml", 
       FileMode.Create,    // the most critical mode-flag
       FileAccess.Write))
{
   doc.Save(stream);
}