Updating data in XML (Linq To XML) generate wrongclosing tags

offroadmidwest

New Member
I have this XmlFile (tasks.xml):\[code\]<?xml version="1.0" encoding="utf-8"?><Tasks> <Task> <Id>dc77b03f-468c-4709-b8dc-4d9741c984dd</Id> <Name>buy paper</Name> <Category>inbox</Category> <DueDate></DueDate> <Project></Project> <Context></Context> <Note></Note> <Created>03/16/2013 15:33:29</Created> <Finished></Finished> </Task></Tasks>\[/code\]And when I update element like this everything works ok:\[code\]try{ var item = (CheckBox)sender; dynamic itemDC = item.DataContext; var folder = ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync("tasks.xml"); var readStream = await FileIO.ReadTextAsync(file); using (var writeStream = await file.OpenStreamForWriteAsync()) { XElement xElem = XElement.Parse(readStream); IEnumerable<XElement> singleTask = from task in xElem.Elements("Task") where (string)task.Element("Id") == itemDC.Id select task; foreach (XElement task in singleTask) { task.SetElementValue("Finished", DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/" + DateTime.Now.ToString("yyyy HH:mm:ss")); } xElem.Save(writeStream); }}catch (Exception ex){ new MessageDialog(ex.Message).ShowAsync();}\[/code\]but if I want to edit \[code\]<Finnished>\[/code\] element again and set value to "" it will generate two wrong closing tags in my XML file.Edit again code:\[code\]try{ var item = (CheckBox)sender; dynamic itemDC = item.DataContext; var folder = ApplicationData.Current.LocalFolder; var file = await folder.GetFileAsync("tasks.xml"); var readStream = await FileIO.ReadTextAsync(file); using (var writeStream = await file.OpenStreamForWriteAsync()) { XElement xElem = XElement.Parse(readStream); IEnumerable<XElement> singleTask = from task in xElem.Elements("Task") where (string)task.Element("Id") == itemDC.Id select task; foreach (XElement task in singleTask) { task.SetElementValue("Finished", ""); } xElem.Save(writeStream); }}catch (Exception ex){ new MessageDialog(ex.Message).ShowAsync();}\[/code\]Wrong generated XML after editing \[code\]<Finished>\[/code\] to "":\[code\]<?xml version="1.0" encoding="utf-8"?><Tasks> <Task> <Id>dc77b03f-468c-4709-b8dc-4d9741c984dd</Id> <Name>dne?n
 
Back
Top