【问题标题】:Add element to an XML file on Windows Phone在 Windows Phone 上将元素添加到 XML 文件
【发布时间】:2012-05-04 19:47:52
【问题描述】:

如何在 Windows Phone 7 中的 XML 文件中添加元素

该文件位于解决方案资源管理器文件夹(“files/IO.xml”)中,因此无需提供有关 IsolatedStorage 的答案。

我的文件是这样的:

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

有什么想法吗?

【问题讨论】:

    标签: xml silverlight linq windows-phone-7


    【解决方案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);
      }
    }
    

    【讨论】:

    • 我不确定您所说的“无需提供有关独立存储的答案”是什么意思,所以我把它省略了。您必须使用独立存储。我编辑了答案。
    • 我应该把那个文件放在哪里,因为我有时需要手动修改它?
    • 我不确定,你想做什么?我听说过用于 PC 的独立存储工具。或者,也许将文件保留在原处,并在启动期间将其复制到 IsolatedStorage。我不确定。
    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多