保存到xml文档

web*_*ad3 4 c# linq linq-to-xml windows-phone-7

我正在" 试图 "弄清楚如何创建Windows Phone 7应用程序,我想用以下函数更新/保存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");
Run Code Online (Sandbox Code Playgroud)

但是xmlDoc.Save行给出了错误:"System.Xml.Linq.XDocument.Save(System.Xml.XmlWriter)"的最佳重载方法匹配具有一些无效参数.

我需要做些什么才能纠正这个问题?

Jon*_*eet 8

您需要保存到隔离存储(或其他一些地方).获取应用程序的独立存储,打开文件流并保存到流:

using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (Stream stream = storage.CreateFile("data.xml"))
    {
        doc.Save(stream);
    }
}
Run Code Online (Sandbox Code Playgroud)