473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove Child Node!

Input Xml:

<foo>
<bar id="1">
<child1>
<innerChild />
...............
</child1>
<child2>
<innerChild />
............
</child2>
</bar>
<bar id="2">
........
</bar>
</foo>

output:
<foo>
<bar id="2">
........
</bar>
</foo>

I want to remove <bar> child of foo with id =1 and all its inner contents.
how can I accomplish this task in C#?

TIA
Nov 12 '05 #1
2 12386
"Petrucci20 00" <no****@microso ft.com> wrote in message
news:Oi******** ******@TK2MSFTN GP09.phx.gbl...
I want to remove <bar> child of foo with id =1 and all its inner contents.
how can I accomplish this task in C#?


1. You need to find it, eg

XmlNode node = doc.SelectSingl eNode( "/foo/bar[@id='1']" ) ;

2. The "Remove" operations will do what you want (they don't unwrap,
they remove the subtree).

So one way:

node.ParentNode .RemoveChild( node ) ;

(An aside: attributes of type ID must be NAMEs, so they're not
supposed to start with numbers. Just in case that's your intention.)

Regards,

-djm
Nov 12 '05 #2
XmlDocument doc=new XmlDocument();
doc.Load(fileNa me);

XmlNode node=doc.Select SingleNode("des cendant::bar[@id='1']");
doc.DocumentEle ment.RemoveChil d(node);

doc.Save(fileNa me);

HTH

Cheers!!

"Petrucci20 00" <no****@microso ft.com> wrote in message
news:Oi******** ******@TK2MSFTN GP09.phx.gbl...
Input Xml:

<foo>
<bar id="1">
<child1>
<innerChild />
..............
</child1>
<child2>
<innerChild />
...........
</child2>
</bar>
<bar id="2">
.......
</bar>
</foo>

output:
<foo>
<bar id="2">
.......
</bar>
</foo>

I want to remove <bar> child of foo with id =1 and all its inner contents.
how can I accomplish this task in C#?

TIA

Nov 12 '05 #3

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

Similar topics

6
3516
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal" and "ns0:Company" but I do not want to delete their child nodes("w:p","w:r","w:t"). How can I do this? <ns0:Proposal> <ns0:Company> <w:p> <w:r>
2
5384
by: Sam | last post by:
I am attempting to remove a full node. XML Example: <Videos> <Video> <Name>Crocodile Dundee 2</Name> <Media>DVD</Media> </Video> <Video> <Name>Terminator 6</Name> <Media>DVD</Media>
7
2604
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
0
1004
by: David | last post by:
Having trouble with System.Windows.Forms.TreeNode. TreeNode parentnode = valid node with child node parentnode.FirstNode.Remove() removes the child node but unfortunately sets parentnode == null. Removing a child seems to nullify the reference to its parent. What is happening here? Using NET 1.1. Thanks.
2
4102
by: daz_oldham | last post by:
Hi Everyone I have an xml document, and for the purposes of my application, I am going to take a node of the xml (child), add a child to it and then store it into the database for later use. So I start with: <doc xmlns:"http://www.xml.com"> <stuff>
4
2913
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a child element with an attribute set at a given value; maybe an example is more self-explaining... Given the following input XML document:
3
2744
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
1
2254
by: SAIRAAM | last post by:
how to remove a child node of a node in a treeview???
11
3306
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a javascript solution to go behind and do a clean up after the page loads. The .NET will leave behind any combination of nested tags. Here is an example below. Even though the <spantags are not empty, as they contain <emtags they also need to be...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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
8851
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
8748
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...
1
6181
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
4175
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...
1
2754
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 we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.