简体   繁体   English

从隔离存储中加载Xml文件-Windows Phone 7

[英]Loading Xml file from Isolated Storage - Windows Phone 7

I'm trying to load xml data saved in the isolated storage, but I always get an error. 我正在尝试加载保存在隔离存储中的xml数据,但是我总是遇到错误。 I use the following code to load xml data saved in the isolated storage 我使用以下代码加载隔离存储中保存的xml数据

 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: xml文件是:

<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); 该错误向我指示的是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. 在尝试读取文件之前,请尝试添加storage.FileExists("Highscores\\\\scores.xml")以确保文件位于独立存储中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM