2010-11-18 76 views
4

我「試圖」弄清楚如何創建一個的Windows Phone 7應用程序,我想更新/保存XML文件具有以下功能:保存到XML文檔

 XDocument xmlDoc = XDocument.Load("myApp.xml"); 

     xmlDoc.Element("ocd").Add(new XElement("vDetails", new XElement("itemName", this.tb_Name.Text), 
      new XElement("Date", System.DateTime.Now.ToString()), new XElement("itemValue", ""))); 

     xmlDoc.Save("data.xml"); 

但是,xmlDoc.Save行提供了一個錯誤:「System.Xml.Linq.XDocument.Save(System.Xml.XmlWriter)的最佳重載方法匹配有一些無效參數。

我需要做什麼以糾正此問題?

回答

8

您需要保存到獨立存儲(或其他一些地方)。獲得獨立存儲您的應用程序,打開一個流文件,並保存到流:

using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (Stream stream = storage.CreateFile("data.xml")) 
    { 
     doc.Save(stream); 
    } 
} 
+0

感謝你爲這個。這個例子把我放在正確的路徑...我想知道,雖然...當我保存文件時,它似乎保存時,我顯示更新的文件。但是,如果關閉「應用程序」,則使用Iso存儲是否會保存並恢復新更新的.xml文件?我似乎無法弄清楚如何測試?有什麼建議麼?也許我需要問這是一個問題。 – webdad3 2010-11-19 16:33:37

+0

@Jeff:孤立的存儲是持久的。您可以將其視爲應用程序自己的專用磁盤。如果應用程序已卸載並重新安裝,我懷疑它會被擦除。 – 2010-11-19 17:24:23

+0

@JonSkeet嗨,這樣你解釋是爲了保存XML文件,但如果我想讀取使用該邏輯的同一文件? 'var xml = XDocument.Load(「arquivo.xml」)。ToString(); VAR內容=新的StringContent( XML, Encoding.GetEncoding( 「ISO-8859-1」), 「應用程序/ XML」);' – 2016-01-03 08:58:30

1

而Windows Phone開發者博客進入談判程序執行模型非常深入。

我認爲區分應用程序'關閉'和一個正在墓碑的應用程序是很重要的。

Application Closing is simply the outcome of the user pressing the hardware Back button enough times to navigate backwards through the pages of your application, past the application’s first page.

Application Deactivated occurs when a different application takes control of the foreground - for example, an incoming phone call, launching a chooser, or the user pressing the Windows button. In both cases, your application will be deactivated (not closed). Before we step into the subtleties of the Deactivated event, let’s make sure we all understand that upon Deactivation, your application gets terminated (at the end). It's that simple; your code can’t run in the background, therefore your application gets terminated. However, unlike an application that is closed, a deactivated application gets tombstoned. Don’t get confused, a tombstoned application’s process still gets terminated. But unlike a closed application, where the WP operating system removes any trace of the application, when an application is deactivated, the WP operating system stores a record (a tombstone) of the application's state. Basically, the WP operating system keeps a tombstone of the application that becomes part of the phone’s application back-stack, which is a journal that enables the use of the hardware Back button to enhance navigation functionality.

Application Execution Model

至於測試,一個想法可能是重構代碼,並添加日誌記錄各種事件點像關閉或正在墓碑等