473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

delete a row from XML file C#

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?
Jul 28 '09 #1
10 10019
GaryTexmo
1,501 Expert 1GB
Have a look at XmlNode.RemoveChild(XmlNode)...
http://msdn.microsoft.com/en-us/libr...movechild.aspx

Hopefully that helps!
Jul 28 '09 #2
@GaryTexmo
content not found....
Jul 29 '09 #3
GaryTexmo
1,501 Expert 1GB
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.
Jul 29 '09 #4
Frinavale
9,735 Expert Mod 8TB
The XmlNode.RemoveChild() method should work for you.

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

-Frinny
Jul 29 '09 #5
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.
Jul 29 '09 #6
Frinavale
9,735 Expert Mod 8TB
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.
Jul 29 '09 #7
GaryTexmo
1,501 Expert 1GB
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, 700 views)
Jul 29 '09 #8
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: http://bytes.com/topic/c-sharp/answe...ge#post3502517 , if u can answer it :)
Jul 31 '09 #9
GaryTexmo
1,501 Expert 1GB
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, 502 views)
Jul 31 '09 #10
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!
Jul 31 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Silvia | last post by:
Hello I have a listview with images, and when I select one image I can delete the image, bu I produce an error, say "the process cannot access the file D:\imagesDicomitzador\142617379\image1.bmp...
3
by: Paul | last post by:
Is it possible to delete a file on a client side machine using VB/JAVA script? My website allows the user to upload a file to the website and after that I would like to delete the file on their...
4
by: Michael K. | last post by:
Hi there I am having trouble deleting an uploaded file that I have just written to disc. I am using the SaveAs method of a HttpPostedFile to write the file to disc, then straight after I'm...
3
by: facicad | last post by:
I would like to know if as event before delete file exist. My probleme is with FileSystemWatch, it is only Deleted event, and went I want copy for bakup my file, the file do not exist :(
2
by: TOI DAY | last post by:
Hi all, How can I delete the file on the server after the user download it? For example: I have file name "123.txt" on a server, I copy it to "ABC.txt", then allow uer download the...
3
by: Parag Gaikwad | last post by:
Hi, I need to delete files across the network using web client (IE 6.x) in Win 2003 - IIS 6.0 environment. Can someone please suggest an approach I can use to acheive this. Will using FSO do...
5
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows...
1
by: ABC | last post by:
Is there any best solution automatic delete file within the special time? My web application can generate many pdf files for users. Because there are many users, I need to control the total...
1
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender,...
4
by: id10t error | last post by:
Hello, I am making a program that will not have an user interaction to delete certian files. I found this line of code. My.Computer.FileSystem.DeleteFile("C:\POLLJP.DWN",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.