嗨:)我有更新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的其他信息:价值不在预期范围内。
请帮我解决问题。感谢您的耐心等待:)。
答案 0 :(得分:0)
尝试使用xml linq ReplaceWith
element.ReplaceWith(new XElement("id", "1234567"));