2012-05-04 35 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

而我应该把那个文件放在哪里,因为我有时需要手动修改它? –