2012-05-04 36 views
0

如何在Windows Phone 7的XML文件中添加元素使用您想要的任何東西(Linq或XmlWriter)我在普通的C#應用​​程序中完成了它,但在Silverlight和WP7中是不同的。將元素添加到Windows Phone上的XML文件

該文件位於解決方案資源管理器文件夾(「files/IO.xml」)中,因此無需提供有關IsolatedStorage的答案。

我的文件是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<lights> 
    <light id="1" name="toto" /> 
    <light id="2" nom="titi" /> 
</light> 

任何想法?

回答

1

假設你的文件是IsolatedStorage,你可以嘗試這樣的事:

using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (IsolatedStorageFileStream isoStore = new IsolatedStorageFileStream("IO.xml", FileMode.Open, store)) 
    { 
    XDocument doc = XDocument.Load(isoStore); 
    doc.Descendants("lights") 
     .FirstOrDefault() 
     .Add(new XElement("light", new XAttribute("id","3"), new XAttribute("name","tete")) 

    doc.Save(isoStore); 
    } 
} 
+0

doc.Save(文件)不wirking! –

+0

我不確定你的意思是「不需要提供關於IsolatedStorage的答案」,所以我把它留下了。你必須使用IsolatedStorage。我編輯了答案。 – Louis

+0

而我應該把那個文件放在哪裏,因爲我有時需要手動修改它? –