Connecting Tech Pros Worldwide Help | Site Map

delete a row from XML file C#

Member
 
Join Date: Apr 2009
Posts: 44
#1: Jul 28 '09
i have XML file like this:
Expand|Select|Wrap|Line Numbers
  1.   <task>
  2.     <status>Waiting</status>
  3.     <time>21:15:38</time>
  4.     <date>Daily</date>
  5.     <works>End Process|^Open URL|http://www.^Remind|1</works>
  6.   </task>
  7.   <task>
  8.     <status>Disabled</status>
  9.     <time>22:35:40</time>
  10.     <date>Daily</date>
  11.     <works>End Process|^Show Message|Hurry, Time is Up!</works>
  12.   </task>
  13.   <task>
  14.     <status>Waiting</status>
  15.     <time>1:35:0</time>
  16.     <date>22/7/2009</date>
  17.     <works>Shutdown</works>
  18.   </task>
  19.  
i want to delete rows which have "Waiting" in status, how to do it?
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Jul 28 '09

re: delete a row from XML file C#


Have a look at XmlNode.RemoveChild(XmlNode)...
http://msdn.microsoft.com/en-us/libr...movechild.aspx

Hopefully that helps!
Member
 
Join Date: Apr 2009
Posts: 44
#3: Jul 29 '09

re: delete a row from XML file C#


Quote:

Originally Posted by GaryTexmo View Post

Have a look at XmlNode.RemoveChild(XmlNode)...
http://msdn.microsoft.com/en-us/libr...movechild.aspx

Hopefully that helps!

content not found....
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#4: Jul 29 '09

re: delete a row from XML file C#


That's odd... I swear it worked yesterday. Let me try again...

http://msdn.microsoft.com/en-us/libr...movechild.aspx

If it doesn't work, type the following into google...
"C# XmlNode.RemoveChild"

It should be the first hit.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#5: Jul 29 '09

re: delete a row from XML file C#


The XmlNode.RemoveChild() method should work for you.

Let us know if it doesn't or if you need more help.

-Frinny
Member
 
Join Date: Apr 2009
Posts: 44
#6: Jul 29 '09

re: delete a row from XML file C#


sorry but i m new to working with XML in C#, i coudlt figure out how can i use the removenode to remove each task with status "Waiting" a little more help would be appriciated please.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#7: Jul 29 '09

re: delete a row from XML file C#


What do you have so far so that we have a common ground to start with?
Please remember to post code in [code] tags so that it makes it easier for us to look at the code and refer to line numbers that may need correcting.
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#8: Jul 29 '09

re: delete a row from XML file C#


I'm guessing by "removenode" you actually mean, "RemoveChild", right?

Anyway, you use it to remove a child node on another XmlNode, which means you need to find that node first. Lets say you've got the following XML structure...

Expand|Select|Wrap|Line Numbers
  1. <items>
  2.   <item>
  3.     <name>Test</name>
  4.     <value>blahblah</value>
  5.   </item>
  6.   <item>
  7.     <name>Valid</name>
  8.     <value>something</value>
  9.   </item>
  10. </items>
... and we want to remove an item with the name of test.

The parent node, in this case, is "items"... lets assume we have an XmlNode already that is this node...

Expand|Select|Wrap|Line Numbers
  1. XmlNode itemsNode = ... 
Now, itemsNode.ChildNodes will contain other XML nodes that refer to each item we have. In the example, there should be two children that are "item" nodes.

Each "item" node will also have two ChildNodes... in our case they will be "name" and "value".

So, to do our removal, we'll want to search through the "items" node to find an "item" child node. If that "item" node has a child node that is "name" and it's inner text* is "Test", we want to remove the "item" node from it's parent, the "items" node. Sorry if that sounds confusing...

*Note, with the node structure you have, the "Test" is actually a child node of the "name" node. However, since it's straight text and not more XML, the InnerText property of the "name" node will be the text we want to find. It's a blatant assumption but for the purposes of this demonstration, it's fine. You may want to have something a little more elegant in your own solution.

Attached is a quick little program I did to demonstrate this with the example XML above. Hopefully it helps you learn about this so you can apply it to your own problem.
Attached Files
File Type: txt Program.cs.txt (2.6 KB, 51 views)
Member
 
Join Date: Apr 2009
Posts: 44
#9: Jul 31 '09

re: delete a row from XML file C#


sry guys its not helping, i use this code when i want to delete a row by its index:
Expand|Select|Wrap|Line Numbers
  1.             DataSet data = new DataSet();
  2.             data.ReadXml(Paths.SetupPath(true) + @"\bin\tasks.xml");
  3.             data.Tables["task"].Rows[taskIndex].Delete();
  4.             data.WriteXml(Paths.SetupPath(true) + @"\bin\tasks.xml");
so i made a loop that goes with every row checks if status == "waiting", code:
Expand|Select|Wrap|Line Numbers
  1.             try
  2.             {
  3.                 DataTable data = Tasks.ReadTasks();
  4.                 int max = data.Rows.Count;
  5.                 for (int loop = 1; loop < max; loop++)
  6.                 {
  7.                     string bah = data.Rows[loop][1].ToString();
  8.                     if (bah == "Waiting")
  9.                     {
  10.                         Tasks.DeleteTask(loop + 1);
  11.                         max--;
  12.                         //loop = 1;
  13.                     }
  14.                 }
  15.             }
  16.             catch { }
this way i loop every task, but it has some problem, i think when it deletes the 1st row with waiting all the row index changes and then everything goes wrong, and it deletes all tasks after the 1st waiting task and sometimes dont delete any task

also see this post: take a screenshot and crop image , if u can answer it :)
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#10: Jul 31 '09

re: delete a row from XML file C#


I'm not familiar with using a DataSet to parse XML, but while I take a look at it, is there any particular reason you're going that route? The example I linked uses the classes available in System.Xml and does pretty much what you're looking for.

Edit:
Ok I took a look at using a DataSet... it's very straightforward. I'd say any problem you're having must come out of your Tasks class, because I used pretty much exactly the same loop as you and it works fine. The key differences are...

Instead of using...
Expand|Select|Wrap|Line Numbers
  1. Tasks.DeleteTask(loop + 1);
I used...
Expand|Select|Wrap|Line Numbers
  1. dt.Rows.RemoveAt(loop)
I don't know how your DeleteTask method works.. but loop+1 is the row ahead of the one you're looking at and removing that would normally cause problems all over the place. Since you say your code runs, I can only assume you're handling that somewhere in DeleteTask.

This is also why I'm hugely in favour of not deleting things on the fly, but rather queuing them up in, say, a list, and then deleting them after you've built the queue. By that I mean...

Expand|Select|Wrap|Line Numbers
  1. // Find things to delete
  2. List<Thing> deleteList = new List<Thing>();
  3. foreach (Thing thing in ThingsToSearch)
  4. {
  5.   if (thing matches delete criteria) deleteList.Add(thing);
  6. }
  7.  
  8. // Delete anything we found
  9. foreach (Thing deleteThing in deleteList)
  10. {
  11.   ThingsToSearch.RemoveThing(deleteThing);
  12. }
  13. deleteList = null;
This means you're not modifying your structure as you loop through it, which means you don't have to keep track of how your structure changes. With more complex structures, it saves you a lot of headache :) There is a cost though, and it's in terms of performance.

Anyway, take another look at your code. As I said, your problem is likely in your Tasks class somewhere. I reproduced your code almost exactly by using the DataSet itself instead of Tasks (which I don't have) and I get the desired result. I've attached my working copy... I don't output the dataset, but if you set breakpoints you can use the Visual Studio DataSet Visualizer to see the changes.
Attached Files
File Type: txt Program.cs.txt (2.2 KB, 25 views)
Member
 
Join Date: Apr 2009
Posts: 44
#11: Jul 31 '09

re: delete a row from XML file C#


anyways i once again tried to understand your code, that one using XML Document.. and used it in my app.. and it is PERFECT

if you would be a girl i would have kissed you :P haha jking

thanks alot!
Reply

Tags
delete, row, xml