I have this result set that I load from an XML file
calendarDocument = XDocument.Load(@"C:\CalendarDB.xml");
IEnumerable<XElement> elements = from e in calendarDocument.Root.Elements("Session") select e;
not lets say I need to delete an element from the calendar file. Will this work?
elements.ToList().Remove(someElement);
calendarDocument.Save(@"C:\CalendarDB.xml");
what about this?
elements.ToList().Remove(someElement);
elements.Last().AddAfterSelf(anotherElement);
elements.Last().AddAfterSelf(yetAnotherElement);
element.ToList().Remove(anotherElement);
calendarDocument.Save(@"C:\CalendarDB.xml");
I have a feeling that every time I call Last() it returns a NEW result set DIRECTLY FROM THE CALENDAR FILE but when I call Remove() it does not remove the element from the file until I call Save() and if I dont call Save() immediately after each Remove() then the next call to Last() will return the same result set as before, still containing the deleted element.
Can someone tell me how this works please. Do I need to call Save() after Remove() each time in order to get the latest from Last()?
You must not call ToList, that creates a copy of the result set and thus removing from it doesn’t affect the original document. To remove an element from the document, just call Remove on the element itself. For example:
The children variable holds enumerable, which is just a definition of a query. Every time you start your query from it, it will restart the query and thus get you the up-to-date results. But you mast start over from children.
There’s no need to commit the changes in any way, they are applied immediately. Only if you want to write the modified document back to a file you will need to call Save on the document.