WP7在独立存储中读写Xml

5

我是WP7的新手。我按照这个教程去读写一个xml文件,但是当我读这个xml文件时,它只显示了xml文件的顶部一行。我不知道如何检查程序是否正确地编写了xml文件。

1.在隔离存储中查找保存的xml文件。

2.如何解决这个问题。

我的代码用于在隔离存储中写入Xml文件:

      using (IsolatedStorageFile myIsolatedStorage =     
                            IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("mz1.xml", FileMode.Create, myIsolatedStorage))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
                {
                    writer.WriteStartDocument();

                    writer.WriteStartElement("person");
                    writer.WriteElementString("node1", "value1");
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
            }
        }

从独立存储中读取XML文件的代码:

          using (IsolatedStorageFile myIsolatedStorage =          
                               IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream isoFileStream =  
                         myIsolatedStorage.OpenFile("mz1.xml", FileMode.Open);
                using (StreamReader reader = new StreamReader(isoFileStream))
                {
                    textBlock1.Text= reader.ReadToEnd();
                }
            }

输出:

     <?xml version="1.0" encoding="utf-8"?>
3个回答

6
作为回答你的第一个问题,你可以从这里的codeplex下载并安装WP7隔离存储浏览器: http://wp7explorer.codeplex.com/ 它非常容易使用。只需在您的app.xaml.cs中添加几行代码即可完成设置。
至于你的第二个问题,你给出的代码看起来没问题。我最近也写了一个小的WP7应用程序,做了同样的事情。以下是一些代码:
public List<Task> GetTasks()
{
    var tasks = new List<Task>();
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists(XmlFile))
        {
            //store.DeleteFile(XmlFile);
            //XDocument doc = XDocument.Load(store.OpenFile(XmlFile, FileMode.Open));
            using (var sr = new StreamReader(new IsolatedStorageFileStream(XmlFile, FileMode.Open, store)))
            {
                XDocument doc = XDocument.Load(sr);
                tasks = (from d in doc.Descendants("task")
                         select new Task
                                    {
                                        Category = (string) d.Attribute("category"),
                                        Id = (string) d.Attribute("id"),
                                        Name = (string) d.Element("name"),
                                        CreateDate = (DateTime) d.Element("createdate"),
                                        DueDate = (DateTime) d.Element("duedate"),
                                        IsComplete = (bool) d.Element("isComplete")
                                    }).ToList<Task>();
            }
        }
    }
    return tasks;
}

由你决定,但你可能需要考虑使用LinqToXml。在我看来,它会让事情变得更加清晰。

我实际上有一篇博客文章介绍了所有这些内容,可以在这里找到:

http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-apppart-2/

你也可以下载所有的代码。 希望对你有所帮助。


谢谢@alex的回复。我安装了资源管理器,在它的文档中说要添加对IsolatedStorageExplorer程序集的引用,但是这个程序集在我的Visual Studio中不存在,尽管我已经安装了资源管理器。 - Mj1992
@Mj1992 - 当你打开添加引用对话框时,只需浏览到库所在的目录,并添加对.dll的引用 - 通常安装在C:\Program Files\WP7 Isolated Storage Explorer\Library中 - 希望有所帮助。 - Alex

2
你的代码执行并且运行良好。我已经将结果从 TextBlock 更改为字符串变量,并输出以下内容:
<?xml version="1.0" encoding="utf-8"?>
<person>
  <node1>value1</node1>
</person>

我猜TextBlock只显示结果的第一行。

1

你是在寻找类似于this?的东西吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接