2012-03-24 26 views
5

我是WP7的新手。 我跟着this教程来读写一个xml文件,但是当我读取xml文件时,它只显示了我的xml文件的第一行。我不知道如何检查天气xml文件是否被程序正确编写。 。WP7在IsolatedStorage中读写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"?> 

回答

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/

,你可以下载所有的代码。 我希望你觉得它有帮助。

+0

日Thnx @alex为reply.I安装Explorer及其文档中,它说添加引用IsolatedStorageExplorer组装,但尽管我已经安装了探险 – Mj1992 2012-03-24 12:44:38

+0

大会没有出现在我的视觉工作室@ Mj1992 - 当您打开添加引用对话框时,只需浏览到该库所在的目录并添加对.dll的引用 - 通常将其引导至安装在C:\ Program Files \ WP7 Isolated Storage Explorer \ Library中 - hth – Alex 2012-03-24 12:49:37

+0

thnx alot @Alex指出我完全忘了那个。 – Mj1992 2012-03-24 12:54:28

2

您的代码执行并正常工作。我已经改变了要在TextBlock的,但字符串变量未设置的结果,它输出以下内容:

<?xml version="1.0" encoding="utf-8"?> 
<person> 
    <node1>value1</node1> 
</person> 

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

+0

是的,你是对的日Thnx – Mj1992 2012-03-24 12:42:58