473,396 Members | 1,860 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,396 software developers and data experts.

C# How to delete element in XML

Hello,
I have this XML file players.xml
Expand|Select|Wrap|Line Numbers
  1. <players>
  2.   <player>
  3.     <name>administrator</name>
  4.     <game>Cube</game>
  5.     <na_tahu>true</na_tahu>
  6.     <score>45</score>
  7.     <partner></partner>
  8.     <last_move></last_move>
  9.   </player>
  10.   <player>
  11.     <name>User2</name>
  12.     <game>Cube</game>
  13.     <na_tahu>true</na_tahu>
  14.     <score>16</score>
  15.     <partner></partner>
  16.     <last_move></last_move>
  17.   </player>
  18. </players>
And I need to delete this whole element
Expand|Select|Wrap|Line Numbers
  1.  <player>
  2.     <name>User2</name>
  3.     <game>Cube</game>
  4.     <na_tahu>true</na_tahu>
  5.     <score>16</score>
  6.     <partner></partner>
  7.     <last_move></last_move>
  8.   </player>
I can identify this element according to child elements <name> and <game>. So if <name>.InnerText="User2" & <game>.InnerText="Cube" then delete <player>...</player>

But I don´t know how to write code in C# for finding that right element player. Could somebody please help me how to delete this element?
Nov 1 '08 #1
2 6413
PRR
750 Expert 512MB
try this ...

Expand|Select|Wrap|Line Numbers
  1. {
  2.                 DataSet ds = new DataSet();
  3.                 ds.ReadXml(@"C:\File.Xml");
  4.                 int rowcount = 0;
  5.                 for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
  6.                 {
  7.                     DataRow r = ds.Tables[0].Rows[i];
  8.                     Console.WriteLine(r[0].ToString() + "");                    
  9.  
  10.                     if (r[0].ToString() == "User2")
  11.                     {
  12.                         DataView drv = new DataView(ds.Tables[0]);
  13.                             //ds.Tables[0] as DataView ;
  14.                         drv[rowcount].Delete();
  15.                         ds.AcceptChanges();
  16.                         ds.WriteXml(@"C:\File1.Xml");
  17.  
  18.                         //ds.Tables[0].Rows[i];
  19.                     }
  20.                     rowcount++;
  21.                 }
  22. File.XML
  23. <?xml version="1.0" encoding="utf-8" ?>  
  24. <players>
  25.    <player>
  26.      <name>administrator</name>
  27.      <game>Cube</game>
  28.      <na_tahu>true</na_tahu>
  29.      <score>45</score>
  30.      <partner></partner>
  31.      <last_move></last_move>
  32.    </player>
  33.    <player>
  34.      <name>User2</name>
  35.      <game>Cube</game>
  36.      <na_tahu>true</na_tahu>
  37.      <score>16</score>
  38.      <partner></partner>
  39.      <last_move></last_move>
  40.    </player>
  41. </players>
  42.  
  43. File1.Xml
  44. <?xml version="1.0" standalone="yes"?>
  45. <players>
  46.   <player>
  47.     <name>administrator</name>
  48.     <game>Cube</game>
  49.     <na_tahu>true</na_tahu>
  50.     <score>45</score>
  51.     <partner />
  52.     <last_move />
  53.   </player>
  54. </players>
  55.  
  56.  
Nov 4 '08 #2
PRR
750 Expert 512MB
You could also serialize and deserialize a class... into a xml file ...
Nov 5 '08 #3

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

Similar topics

2
by: Michal Januszczyk | last post by:
Hello. Sometimes I encounter the following problem: foreach( element of some collection) { delete this element; } of course, the runtime would not allow to modify
14
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy...
4
by: frizzle | last post by:
Hi there, i have a soccer-site, on which of course, can be kept track of played matches. Now the admin has to be able to set the number of goals, and then select who scored what goal. I want...
2
by: richardkreidl | last post by:
I want to be able to delete and search for elements in a XML file, I'm using the code below for adding elements which works great: Public Sub cmdAddElement_Click(ByVal sender As System.Object,...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
3
by: AbrahamLincolnIllinois | last post by:
Hi all. I have a list of pointers to a complicated object. When I erase() a member of that list, the little blob of memory that contains the pointer is deleted, I think. But the object pointed...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.