开发者

IsolatedFileStorage XML Reading Crash

Ok so, basically my problem is with reading and XML file from IsolatedFileStorage. I'll go through the process that leads to my error and then I'll list the relevant code and XML file.

  1. On the first execution it recognises that the file does not exist - it therefore creates the file in IsolatedFileStorage
  2. On the second execution it can now see that the file does exist and so it loads the XML file
  3. On the third execution it can see that it exists - but it throws an XML error

I cannot for the life of me find a solution to it (link to other discussion on MSDN here)

So the code for reading/creating the XML file in IsolatedFileStorage is as follows:

try
{
      /***********************
       * CHECK THE SETTINGS
       ********************/
       if (store.FileExists("AppSettings.xml"))
       {
            streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Open, store);
            DebugHelp.Text = "AppSettings.xml exists... Loading!";
            streamSettings.Seek(0, System.IO.SeekOrigin.Begin);
            xmlDoc = XDocument.Load(streamSettings, LoadOptions.None);
       }
       else
       {
            streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Create, store);
            DebugHelp.Text = "AppSettings.xml does not exist... Creating!";
            xmlDoc = XDocument.Load("AppSettings.xml", LoadOptions.None);
       }

       if (xmlDoc != null)
            xmlDoc.Save(streamSettings);
}
catch (Exception e)
{
       DebugHelp.Text = e.ToString();
}
finally
{
 开发者_如何学编程      streamSettings.Close();
}

And the related XML file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
</Settings>

Extremely advanced I know - however it throws the following error (here) and you can find the full error text at the bottom of the Social.MSDN page.

Please help - I have been looking for a solution (as the one on the social.msdn site didn't work) for about 2 weeks now.


Why don't you try to read file using a simple StreamReader ? Below a part of a method I have created to readfile from store. Have a try, check your content, and then try loading xml from String (XDocument.Parse etc ...)

String fileContent = String.Empty;

using (_store = IsolatedStorageFile.GetUserStoreForApplication())
{
     if (_store.FileExists(file))
     {
         _storeStream = new IsolatedStorageFileStream(file, FileMode.Open, _store);
         using (StreamReader sr = new StreamReader(_storeStream))
         {
              fileContent = sr.ReadToEnd();
         }
         __storeStream.Close();

         return fileContent;
     }
     else {
        return null;
     }
}


It looks to me like the problem is in your save method - it looks like you are maybe appending the settings each time you close - to overwrite your existing settings, you need to ensure that you delete your existing file and create a new one.

To help debug this, try using http://wp7explorer.codeplex.com/ - this might help you see the raw file "on disk"

As an aside, for settings in general, do check out the AppSettings that IsolatedStorage provides by default - unless you have complicated needs, then these may suffice on their own.


Your code sample isn't complete so it's hard to say for sure but, rather than just seeking to the start of the file you may find it easier to just delete it if it already exists. You can do this with FileMode.Create. In turn this means you can do away with the need to check for the existing file.

I suspect that the problem is that you are writing a smaller amount of text to the file on subsequent attempts and so leaving part of the original/previous text behind. In turn this creates a file which contains invalid XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜