Delete Data from XML File Using C#

by percent20 27. December 2007 17:04

Part 1: Write to XML File Using C#
Part 2: Reading Data from XML File Using C#
Part 3: Delete Data from XML File Using C#

In the last post on XML, Reading Data from XML File Using C#, we retrieved our data. The only thing we really have left to do is delete data.

I am going to leave updateing data to you to figure out. With these 3 main posts it should be enough for you to figure out how to update data. If not please feel free to leave a comment and I will go ahead and make a post on it, eventually.

The Code


DeleteContact() method

How about we step through this short amount of code. Please, if you haven't taken a look at the other two posts I stongly advise you to.

public void DeleteContact(string theGuid)

In Write to XML File Using C# we mentioned the Guid was going to be used to find unique data to delete out of the xml file, similar to a database. Here we are taking the guid as a sting parameter to compare to all the others guids that we get from our xml file.

XmlNode rootContacts = doc.SelectSingleNode("//contacts");
XmlNodeList nodes = rootContacts.SelectNodes("contact");

Briefly, this gets the main contacts node and takes all the main nodes under that and adds them to a list that we can iterate through. This is explained more in: Reading Data from XML File Using C#.

for(int i = 0; i < nodes.Count; i++)

We are setting up a basic for loop to iterate through our list. We are using a for loop because we can't delete anything out of a list while using a foreach. We start at 0 since most collects start at a base of 0. We are using i < nodes.Count because the "Count" is the total number of objects in the collection and starts at 1 so we don't want to include it with <=.

if(nodes[i].SelectSingleNode("Guid").InnerText == theGuid)

This code is comparing the text of the node Guid inside of the contact node to our parameter we passed to the method.

rootContacts.RemoveChild(nodes[i]);

If the 2 guid's are the same then this line of code deletes that contact node as a whole.

doc.Save(_path);

Again this just saves the current xml file, but this time with the data gone.

Wrap-Up

This was a quick post on how to delete data. If you have followed along with Part 1 and Part 2 then this should be pretty simple. If you haven't looked at them please do.

Any questions, comments, or suggestions are welcome please leave a comment.

XmlAccess.zip (2.22 kb)

Tags: ,

Comments

12/30/2007 5:13:27 PM #

Chris Patterson

Why not just do:

XmlNode contacts = documentElement.SelectSingleNode(@"/contacts");
XmlNode contact = contacts.SelectSingleNode(@"/contacts/contact(@Guid == " + theGuid + ")");
contacts.RemoveChild(contact);

Chris Patterson United States

1/22/2008 1:06:32 PM #

pingback

Pingback from weblogs.asp.net

I have arrived - Beginnermediate ASP.NET

weblogs.asp.net

12/10/2008 3:31:39 AM #

Danny Hansen

This should be the correcr Xpath query. The other one searches on a attribute named guid. In this case you are looking for a element guid in the element contact. Also the () need to be replaced by []. () are not working in my code.

XmlNode contacts = documentElement.SelectSingleNode(@"/contacts");
XmlNode contact = contacts.SelectSingleNode(@"/contacts/contact[Guid == " + theGuid + "]");
contacts.RemoveChild(contact);

Danny Hansen Netherlands

5/15/2009 4:11:32 PM #

sulumits retsambew

great post, thanks for sharing.

sulumits retsambew United States

5/27/2009 7:46:00 PM #

roushan

awesome

roushan India

6/28/2009 7:06:34 AM #

wisata ciamis

The template of this blog is quite good, and I think the score of this blog is 8.7 and I will vote for you in Blogs Choice Awards

wisata ciamis United Kingdom

6/30/2009 2:00:12 AM #

confessionblues

hi nice to meet you, and i like youre tutor i learn from here thanks

regard

confessionblues Indonesia

6/30/2009 3:59:39 AM #

refurbished xbox 360

xbox microsoft is a new game

refurbished xbox 360 United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Something about the author