【问题标题】:How to update XML in Windows Phone 8.0如何在 Windows Phone 8.0 中更新 XML
【发布时间】:2015-02-28 19:33:58
【问题描述】:

嗨 :) 我在更新嵌入在 XML 中的元素时遇到问题。我可以通过代码加载 XML:

string _filePath = @"AppSources/UserConfig.xml";
StreamResourceInfo xml = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));
var doc = XElement.Load(xml.Stream);
var id = doc.Elements(XName.Get("id")).SingleOrDefault().Value;

当我尝试更新此文件中的元素时会出现问题。我写了代码:

Uri uri = new Uri("ms-ms-resource://" + _filePath, UriKind.RelativeOrAbsolute);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); //EXCEPTION HERE

using (var stream = await file.OpenStreamForWriteAsync())
{
    var doc = XDocument.Load(stream);
    var element = doc.Descendants("id").FirstOrDefault();
    element.Value = "1234567";

    doc.Save(stream);
}

问题在于代码行:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

我得到了异常:'System.ArgumentException' 来自 VS 的附加信息:值不在预期范围内。

请帮助我解决问题。谢谢你的耐心:)。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    尝试使用xml linq ReplaceWith

    element.ReplaceWith(new XElement("id", "1234567"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多