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

delete nodes in XML

Shinobi
I am trying to delete a node in xml. Nothing is worked correctly.
Here is XML
Expand|Select|Wrap|Line Numbers
  1. <cat>
  2.   <category>
  3.     <id>1</id>
  4.     <name>Architecture</name>
  5.   </category>
  6.   <category>
  7.     <id>2</id>
  8.     <name>Education</name>
  9.   </category>
  10.  
I tried this code. But not worked.
Expand|Select|Wrap|Line Numbers
  1. XmlDocument xdoc = new XmlDocument();
  2.         xdoc.Load(Server.MapPath("main.xml"));
  3.         XmlNodeList nodes = xdoc.GetElementsByTagName("category");
  4.         foreach (XmlNode node in nodes)
  5.         {
  6.  
  7.             if (node["id"].InnerText == id)
  8.             {
  9.                 node.ParentNode.ParentNode.RemoveChild(node.ParentNode);
  10.             }
  11.  
  12.         }
Please help me.

Thanks.
Dec 9 '08 #1
3 11379
nukefusion
221 Expert 100+
You look like you've got too many ParentNode's in that last bit. Try:

Expand|Select|Wrap|Line Numbers
  1. XmlDocument xdoc = new XmlDocument();
  2. xdoc.Load(Server.MapPath("main.xml"));
  3. XmlNodeList nodes = xdoc.GetElementsByTagName("category");
  4. foreach (XmlNode node in nodes)
  5. {
  6.     if (node["id"].InnerText == "1")
  7.     {
  8.         node.ParentNode.RemoveChild(node);
  9.     }
  10. }
  11.  
A slightly better implementation might be to use XPath to find the target node, rather than looping through lots of nodes. Something like:

Expand|Select|Wrap|Line Numbers
  1.           XmlDocument xdoc = new XmlDocument();
  2.             xdoc.Load(Server.MapPath("main.xml"));
  3.             XmlNode childToDelete = xdoc.SelectSingleNode("/cat/category[id=\"1\"]");
  4.             childToDelete.ParentNode.RemoveChild(childToDelete);
  5.  
Dec 9 '08 #2
Thanks. Yhank u so much
Dec 10 '08 #3
PRR
750 Expert 512MB
@Shinobi
In addition to what nukefusion explained... you could also use a dataset...Dataset Read Write XML
Dec 11 '08 #4

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

Similar topics

8
by: Andrew Edwards | last post by:
The following function results in an infinite loop! After 5+ hours of debugging, I am still unable to decipher what I am incorrectly. Any assistance is greatly appreciated. Thanks in advance,...
4
by: Martin Magnusson | last post by:
I'm getting segmentation faults when trying to fix a memory leak in my program. The problem is related to lists of pointers which get passed around between objects. Here is a description of how...
3
by: Earl Teigrob | last post by:
I am considering writing a Class that Selects, Adds, Updates and Deletes Nodes in an XML File but do not what to reinvent the wheel. (See XML file below) That data format would emulate records...
2
by: Manny Chohan | last post by:
Hi Guys, Can someone please suggest how i can delete XML file? I used File.Delete method however it only deleted the nodes inside the file but the file still exists? Thanks Manny
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
4
by: Jonas Ferreira | last post by:
Hi everyone, I'm trying to code a linked list example and everything was working fine. Then I started to code my clear() (to clear my list) but it won't work. Here is the code: class list {...
5
by: nandor.sieben | last post by:
I am using a small set of functions that implements an n-ary tree. The library is disscusses here: ...
6
by: Kindler Chase | last post by:
I'm trying to iterate through a set of nodes and then edit/delete specific attributes using XPathNodeIterator. Adding attributes is no problem. My first question is how do I delete an attribute...
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form with a (continuous) subform, and on the subform I...
31
by: matthewslyman | last post by:
I have an unusual design and some very unusual issues with my code... I have forced Access to cooperate on everything except one issue - record deletion. My form design involves a recursively...
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: 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:
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?
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
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.