473,661 Members | 2,464 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><Appl e Color = "White"/></BadApple>
<BadApple><Appl e 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 1465
The usual problem of changing the list you are enumerating...

how about:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<x ml><Fruits><App le Color = \"Red\"/><Apple Color
= \"Blue\"/><BadApple><App le Color =
\"White\"/></BadApple><BadAp ple><Apple Color =
\"Green\"/></BadApple><Apple Color = \"Maroon\"/></Fruits></xml>");
List<XmlElement remove = new List<XmlElement >();
foreach (XmlElement el in doc.GetElements ByTagName("BadA pple"))
{
remove.Add(el);
}
foreach (XmlElement el in remove) {
el.ParentNode.R emoveChild(el);
}
Console.WriteLi ne(doc.OuterXml );

Note I used GetElementsByTa gName, 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.c omschreef in bericht
news:11******** **************@ m7g2000cwm.goog legroups.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><Appl e Color = "White"/></BadApple>
<BadApple><Appl e 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
3125
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 child node were trivial). Does anyone know the solution to this problem? void CTree::Delete(CPerson *&pPerson)
12
1829
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 the function several times if there are many keyword nodes. Why? function removeKeywords() { var form_obj = window.opener.document.getElementById('myform');
2
1534
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 is required). I have implemented all the Regex stuff onload already. function checkRequired(varForm){ var varReturn = true; for(var i=0; i<varForm.length; i++){ if(varForm.elements.className.indexOf('required') != -1 &&\...
3
16034
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
2084
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 .Net's XML Namespace and was wondering if someone could help with something....in the VB 6 application, we would append two extra nodes to the XMLRecordset-- an error message, and error number. Then, when it came time to reconstruct the recordset...
2
10686
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 attribute and remove the containing node (and child nodes) if it has a certain value. I'm able to find the attributes using an XmlTextReader. Once found, can someone help me get the XPath at that point? I would then use this to remove the node from...
1
11215
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 find them. The problem is, this gets really slow for complicated documents. Is there a better way to either: a) Get a flat list of all the text nodes in the document (like getElementsByTagName, only for text nodes) b) Run a regex replace on...
3
7266
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, .Net likes to add these namespaces: <RootNode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ChildNode xmlns="MyObjectHierarchyNamespace/> </RootNode> The problem this is causing me is that...
5
7815
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 the actual xml nodes about by interacting with the non-xml TreeView nodes. my basic algorithm is as follows: user has selected a treeview node if user presses Ctrl+Up Arrow, then
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.