2014-06-08 21 views
0

我正在將xml文件的內容完美地讀入附有輕擊事件的longlistselector中。所有工作都很棒。該文件位於項目的主資源文件夾中。無法將字符串/節點保存到XML WP8

現在我還想爲我的簡單XML添加字符串/節點,但出於某種原因,我無法找到將其保存到文件的正確語法。

我的XML文件看起來像:

<?xml version="1.0" encoding="utf-8" ?> 
<phrases> 
<item><name>What is your name?</name></item> 
<item><name>How old are you?</name></item> 
</phrases> 

現在,我嘗試了按鈕的單擊事件內的下列內容:

XDocument xDoc = XDocument.Load("phrases.xml"); 
var contactsElement = new XElement("item", 
            new XElement("name", "blalllllaaaallaala"))); 
xDoc.Add(contactsElement); 
xDoc.Save("phrases.xml"); 

VS2013告訴我,xDoc.Save(「短語。 xml「)有無效的參數。當我從該文件中讀取時,我提供了相同的路徑,所以我不明白這裏預期的是什麼?請提出一些建議。

+0

您確定您擁有該文件的寫入權限嗎? –

+0

我不確定。我該如何檢查,如何給予寫入權限? – SunnySonic

回答

1

剛剛嘗試了這個snippet ...

// load original XML from the stream 
XDocument loadedData = XDocument.Load(stream); 

// create a new parent XML structure (new root) and load the original nodes 
var newXml = new XDocument(new XElement("Histories")); 
newXml.Root.Add(loadedData.Root); 

// create the new node 
var contactsElement = new XElement("item", 
           new XElement("name", "blalllllaaaallaala"))); 
NewNode.Add(contactsElement); 

// add the new node 
newXml.Root.Add(NewNode); 

// save the stream 
newXml.Save(stream); 

詳細看看here了。

+0

感謝您的回答,它會工作!問題是該文件在資產文件夾中。雖然失敗應該更清楚,但沒有寫入權限。我必須先將它複製到獨立存儲器,然後從那裏處理文件。 – SunnySonic