简体   繁体   English

WP8:丢失数据或访问隔离存储中的XML文件

[英]WP8: Losing data or access to XML file in isolated storage

I am creating a WP8 shopping list app that stores user created lists(in my code, each shopping list is defined as a ListObj that I defined). 我正在创建一个WP8购物清单应用程序,该应用程序存储用户创建的清单(在我的代码中,每个购物清单都定义为我定义的ListObj)。 I would like to save the lists created by users through an .xml file. 我想保存用户通过.xml文件创建的列表。 As long as I continue to add to this list of ListObj's, I seem to have no issues. 只要我继续添加到ListObj的列表中,我似乎就没有问题。 But I start to experience trouble when I want to remove a ListObj from my list. 但是,当我想从列表中删除ListObj时,我开始遇到麻烦。 When I reopen my app after I removed something from my list of ListObj's and attempt to load my list upon start, I enter this try/catch block: 当我从ListObj的列表中删除某些内容并尝试在启动时加载列表后重新打开应用程序时,我输入以下try / catch块:

try
{
  using (IsolatedStorageFile appStorage =
           IsolatedStorageFile.GetUserStoreForApplication())
  {
    if (appStorage.FileExists("rootList.xml"))
    {
      using (IsolatedStorageFileStream isStream = 
               appStorage.OpenFile("rootList.xml", 
                                  FileMode.Open, FileAccess.Read))
      {
        XmlSerializer serializer = new XmlSerializer(typeof(List<ListObj>));
        rootList = (List<ListObj>)serializer.Deserialize(isStream);
      }
    }
    else
    {
      rootList = new List<ListObj>();
      Debug.WriteLine("rootList not found.");
    }
  }
}
catch
{
  ///Uhhh....
}

However, my program executes the catch statement in which nothing happens obviously. 但是,我的程序执行的catch语句中显然没有任何反应。 I am unsure of what to execute in this catch block in order to diagnose my problem. 我不确定在该catch块中执行什么以诊断我的问题。 I think I am losing access to the app's Isolated storage but again, I am unsure as to how to proceed. 我想我失去了访问该应用程序的独立存储的权限,但同样,我不确定如何进行操作。 Any ideas? 有任何想法吗?

So I ended up finding a solution to my problem. 因此,我最终找到了解决问题的方法。 It turns out that the way I modified my .xml file caused an error in which it can no longer be read. 事实证明,我修改.xml文件的方式导致了一个错误,导致无法再读取该文件。 I solved this by completely overwriting my file with the modified data rather than trying to change the existing data. 我通过使用修改后的数据完全覆盖文件而不是尝试更改现有数据来解决此问题。

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

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