How can I update an element of an existing XML file in c# Windows 8 App? -
i have xml file located in assets folder. face no problem when tried read file using linq. however, when wanted write file see no error , target element never gets updated.
my codes below:
private static readonly string mealsxmlpath = path.combine(package.current.installedlocation.path, "assets/meals.xml"); private xdocument loadeddata;
in constructor have initialisation code:
loadeddata = xdocument.load(mealsxmlpath);
the actual code using update existing element value here
public async void updatequantity(dictionary<int, int> orderdetails) { xdocument xmlfile = xdocument.load(mealsxmlpath); foreach(var key in orderdetails.keys) { xelement upd = (from order in xmlfile.descendants("meal") order.element("id").value == key.tostring() select order).single(); upd.element("avaibility").value = (convert.toint32(upd.element("avaibility").value) - orderdetails[key]).tostring(); } }
i have tried other built in methods such xdoc.save("path")
, xml...create(..)
.but none seems working in c# windows 8 app.
you have save document writable directory :
using (var stream = await (await applicationdata.current.localfolder.createfileasync(system.io.path.getfilename(mealsxmlpath))).openasync(fileaccessmode.readwrite)) { xmlfile.save(stream.asstreamforwrite()); }
Comments
Post a Comment