1
votes

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.GetUserStoreForApplication();

        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.

1

1 Answers

0
votes

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.