2012-03-24 50 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