0

I have an existing xml file which I want to update.

using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
        {
            // Load the xml document for reading
            using (IsolatedStorageFileStream ISFS = ISF.OpenFile(FILE_NAME, FileMode.Open, FileAccess.ReadWrite))
            {
                XDocument file= XDocument.Load(ISFS);
                //Edit the file
                file.Save(ISFS);
            }
        }

The problem is whenever I use file.Save(ISFS), and then try to load it again, using same method, an error is reported

  System.Xml.XmlException was unhandled by user code
  HResult=-2146232000
  Message=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. Line 25, position 14.

UPDATE :

Here's the file after saving

<?xml version="1.0" encoding="utf-8"?>
<A>
   <AB ID="0">
       <NickName>Dummy</NickName>
   </AB>
</A><?xml version="1.0" encoding="utf-8"?>
<A>
   <AB ID="0">
       <NickName>Dummy</NickName>
   </AB>
   <AB ID="1">
       <NickName>My1</NickName>
   </AB>
</A>
Pankaj Parag
  • 437
  • 1
  • 6
  • 17
  • [This](http://stackoverflow.com/questions/9408262/adding-new-xelement-appends-entire-xml-file-to-existing-file-when-saved) was very helpful. – Pankaj Parag Jan 23 '13 at 07:49
  • I have a workaround. Since I don't get a part of the document but the whole document itself, so I delete the old document and create a new file. There's an extra overhead of deleting and writing of the file and also risk of losing data if the app crashes after deleting but before creating the file. – Pankaj Parag Jan 25 '13 at 20:47
  • you can just merge all and new – COLD TOLD Jan 25 '13 at 20:53

0 Answers0