How to delete a item in xml file

shibs

New Member
Hi I need to delete an Item in a XML file, I can add to it no problem but would like to know how to adjust one Item in the file. I need to delete the Item with the name Mike in it and the date, how do I achieve this?Edit: I'm getting a \[code\]NullReferenceException\[/code\]here is my XML\[code\]<Items> <Item> <Name>Mike</Name> <Date>5/4/2000</Date> </Item> <Item> <Name>Martin</Name> <Date>5/4/2010</Date> </Item></Items>\[/code\]This is the code I am trying\[code\]public void deleteElement() { //Get users private store info IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream isoStream; //open selected file isoStream = new IsolatedStorageFileStream("Item.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read, isoStore); XDocument xml = XDocument.Load(isoStream); isoStream.Close(); //Find section XElement sectionElement = xml.Descendants("Item").Where(c => c.Attribute("Name").Value.Equals("Mike")).FirstOrDefault(); //Find item and remove it sectionElement.Elements("Item").Where(c => c.Attribute("Name").Value.Equals("Mike")).FirstOrDefault().Remove(); isoStream.Close(); //Seems unnecessary but it's needed. //Write xml file isoStream = new IsolatedStorageFileStream("Item.xml", FileMode.Create, FileAccess.Write, isoStore); xml.Save(isoStream); isoStream.Close(); }\[/code\]I would appreciate if you could help me thanks.
 
Back
Top