M_K
M_K

Reputation: 3455

How to delete a item in xml file

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 NullReferenceException

here is my XML

<Items>
 <Item>
  <Name>Mike</Name>
  <Date>5/4/2000</Date>
 </Item>
 <Item>
  <Name>Martin</Name>
  <Date>5/4/2010</Date>
 </Item>
</Items>

This is the code I am trying

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


    }

I would appreciate if you could help me thanks.

Upvotes: 1

Views: 3850

Answers (3)

Chuck Savage
Chuck Savage

Reputation: 11955

Using this Xml Library you can write it like:

XElement mike = xml.Root.XPathElement("Item[Name='Mike']");
mike.Remove();

Upvotes: 1

yamen
yamen

Reputation: 15618

xml.Root.Elements("Item")
        .Where(item => item.Element("Name").Value == "Mike")
        .FirstOrDefault()
        .Remove();

Produces:

<Items>
  <Item>
    <Name>Martin</Name>
    <Date>5/4/2010</Date>
  </Item>
</Items>

Note a few things:

  • Started searching for direct elements under the root element ("Items") called "Item", but not their children.
  • Under the item element, look for the "Name" element (not attribute) with the relevant value ("Mike")

Upvotes: 1

erikxiv
erikxiv

Reputation: 4075

From a cursory glance it seems as if you are trying to retrieve the same element twice.

// Retrieve the desired Item element
XElement sectionElement = xml.Descendants("Item").Where(c => c.Attribute("Name").Value.Equals("Mike")).FirstOrDefault();
// Retrieve (and remove) the desired Item element
// This line will fail, as there is no Item descendants of the Item element
sectionElement.Elements("Item").Where(c => c.Attribute("Name").Value.Equals("Mike")).FirstOrDefault().Remove();

How about just using one line?

xml.Descendants("Item").Where(c => c.Attribute("Name").Value.Equals("Mike")).FirstOrDefault().Remove();

BTW: I do not recognize the methods used, but "Name" is probably not an attribute, but rather an element.

Upvotes: 0

Related Questions