473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing xml nodes...why is it so complicated

RC
Hi,
>From the following xmldocument, I want to remove all the badapple xml
nodes..It looks like it is not straight forward .NET 2.0 using
xmldocument and xpath

Input:
<xml>
<Fruits>
<Apple Color = "Red"/>
<Apple Color = "Blue">
<BadApple><Apple Color = "White"/></BadApple>
<BadApple><Apple Color = "Green"/></BadApple>
<Apple Color = "Maroon">
</Fruits>
</xml>

Output:
<xml>
<Fruits>
<Apple Color = "Red"/>
<Apple Color = "Blue">
<Apple Color = "Maroon">
</Fruits>
</xml>

thanks in advance..

Nov 12 '06 #1
2 1454
The usual problem of changing the list you are enumerating...

how about:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<xml><Fruits><Apple Color = \"Red\"/><Apple Color
= \"Blue\"/><BadApple><Apple Color =
\"White\"/></BadApple><BadApple><Apple Color =
\"Green\"/></BadApple><Apple Color = \"Maroon\"/></Fruits></xml>");
List<XmlElementremove = new List<XmlElement>();
foreach (XmlElement el in doc.GetElementsByTagName("BadApple"))
{
remove.Add(el);
}
foreach (XmlElement el in remove) {
el.ParentNode.RemoveChild(el);
}
Console.WriteLine(doc.OuterXml);

Note I used GetElementsByTagName, but a SelectNodes implementation
would have been otherwise identical.

Marc

Nov 12 '06 #2
RC,

Did you ever use the XMLNodeReader it is for me always more sufficient,

http://msdn.microsoft.com/library/de...ClassTopic.asp

I hope this helps,

Cor

"RC" <sr****@gmail.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
Hi,
>>From the following xmldocument, I want to remove all the badapple xml
nodes..It looks like it is not straight forward .NET 2.0 using
xmldocument and xpath

Input:
<xml>
<Fruits>
<Apple Color = "Red"/>
<Apple Color = "Blue">
<BadApple><Apple Color = "White"/></BadApple>
<BadApple><Apple Color = "Green"/></BadApple>
<Apple Color = "Maroon">
</Fruits>
</xml>

Output:
<xml>
<Fruits>
<Apple Color = "Red"/>
<Apple Color = "Blue">
<Apple Color = "Maroon">
</Fruits>
</xml>

thanks in advance..

Nov 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3119
by: Jimmy | last post by:
Hi everyone, I am working with a binary tree, and I am having a bit of trouble visuallizing what needs to happen when I am trying to delete a node that has two children. (no child node and one...
12
1818
by: Ole Noerskov | last post by:
The function below is supposed to remove all childnodes with a name that starts with "keywords" in "myform" in the window.opener. It works fine if there's one keyword node. But you have to run...
2
1525
by: bmgz | last post by:
I have written a simple function that validates a form based on the form objects' className attribute. The form basically write a "field required" message next to the form element that is blank(and...
3
16019
by: e-mid | last post by:
Here is an xml structure. i want to remove <a> nodes that do not have any child. How can i do that in csharp? <root> <a> <b/> </a> <a/> <a/> <a> <c/>
1
2079
by: Jim Bancroft | last post by:
Hi, I'm porting a VB 6 app to .Net, and wanted to use an XMLDataDocument to store information that had previously been kept in XML Recordsets. Unfortunately, I'm not too familiar yet with...
2
10662
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
1
11183
by: Jeremy | last post by:
I have a script that runs a regular expression replace on all text in the document. Currently, I recurse the entire document looking for text nodes and run the replacement on the text nodes when I...
3
7252
by: Keith Patrick | last post by:
I'm doing some document merging where I want to bring in an XmlDocument and import its document element into another document deeper in its tree. However, when serializing my underlying objects,...
5
7793
WebDunce
by: WebDunce | last post by:
Hi guys, I am trying to develop a simple xml editor. I have an object that has a TreeView. I use the TreeView to display the Xml node info. That's all fine, but i want the user to be able to move...
0
7114
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
7321
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
5623
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,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.