473,385 Members | 1,569 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,385 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 6411
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.