开发者

Loading Xml file from Isolated Storage - Windows Phone 7

开发者 https://www.devze.com 2023-04-08 02:30 出处:网络
I\'m trying to load xml data saved in the isolated storage, but I always get an error. I use the following code to load xml data saved in the isolated storage

I'm trying to load xml data saved in the isolated storage, but I always get an error. I use the following code to load xml data saved in the isolated storage

 IsolatedStorageFile storage = IsolatedStorageFile.Ge开发者_JAVA百科tUserStoreForApplication();

        storage.CreateDirectory("Highscores");

        using (var isoFileStream = new IsolatedStorageFileStream("Highscores\\scores.xml", FileMode.OpenOrCreate, storage))
        {
            using (XmlReader reader = XmlReader.Create(isoFileStream))
            {
                XDocument xml = XDocument.Load(reader);
                int i = 0;

                foreach (var score in xml.Root.Element("Highscores").Elements())
                {
                    Count_to_10.Page2.Highscores.scores[i++] = score.Value.ToString();
                }

            }
        }

But I get the following error

Root element is missing.

in this line

XDocument xml = XDocument.Load(reader);

The xml file is:

<HighscoreTable>
  <Highscores length="25">
    <score>00:00:09.000</score>
    <score>00:00:07.000</score>
    <score>00:00:02.000</score>
    <score>00:00:04.000</score>
  </Highscores> 
</HighscoreTable>

I'd be glad if you'd help me find the source of the error.


What that error indicates to me is the XDocument.Load(reader); call is trying to read the file given and cannot find the file. Essentially your file was not ever saved to isolatedstorage in the first place or it was saved with a different path.

I was testing something for myself and I was able to duplicate your issue when I tried to read the wrong file path.

Try adding storage.FileExists("Highscores\\scores.xml") to make sure your file exists in isolatedstorage before you try to read it.

0

精彩评论

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

关注公众号