How to update XML in Windows Phone 8.0



Hi :) I have the problem with updating element embeded in XML. I can load XML by code:



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;


The problem occurs when i try to update element in this file. I wrote the code:



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);
}


I get the exception : 'System.ArgumentException' Additional information from VS: Value does not fall within the expected range.


Please help me in solving the problem. Thank you for patience :).


No comments:

Post a Comment