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

How to remove an XML Node

Hi, I am having trouble with deleting nodes from an XML file. I use the
following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

<country name="France">
<city name="Paris">
<street name="1 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
<street name="2 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
</country>

The code seems to work except it is not removing the full node. I.E it
leaves behind and empty tag.

For example, if I needed to delete the second street address in the XML
above (2 France Lane) it would leave an empty tag of <street></street>
behind as illustrated below.

<country name="France">
<city name="Paris">
<street name="1 France Lane">
<details>
<postcode>9988jj</postcode>
<province></province>
<c>France</c>
</details>
</street>
<street>
</street>
</country>

Can anyone advise how I can clean this up and remove the full Street
element?

Many thanks.

Mark.
Jun 14 '07 #1
2 2303
mark wrote:
Hi, I am having trouble with deleting nodes from an XML file. I use the
following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()
RemoveAll remove attributes and children but not the node itself. To do
that use e.g.
xNode.ParentNode.RemoveChild(xNode)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 14 '07 #2
Excellent, thanks Martin that worked.

Martin Honnen wrote:
mark wrote:
>Hi, I am having trouble with deleting nodes from an XML file. I use
the following code to remove an XML node.

xNode = xDoc.SelectSingleNode("//country/city/street[@name='" &
ListBox1.SelectedItem.ToString & "']")
xNode.RemoveAll()

RemoveAll remove attributes and children but not the node itself. To do
that use e.g.
xNode.ParentNode.RemoveChild(xNode)
Jun 14 '07 #3

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

Similar topics

3
by: Girish | last post by:
I have this XML FILE where I am reading data from and it has this node doctype "< ! D O C T Y P E a d f S Y S T E M " h t t p : / / w h o s c a l l i n g . c o m / d t d / a d f d t d . d t d...
1
by: Steve S | last post by:
I need some help modifying this code. The code was orginally setup to add/remove a single table row. I modified it to add 2 (two) rows with one column that spans both rows. That part works. I...
6
by: Boobie | last post by:
Is there any other way to remove a node/element other than going into the Parent and do "removeChild" ? thanks (need to remove a div of id "foobar")
2
by: Petrucci2000 | last post by:
Input Xml: <foo> <bar id="1"> <child1> <innerChild /> ............... </child1> <child2> <innerChild />
6
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"...
2
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>
2
by: William | last post by:
Have somebody a brilliant idea how to remove a nodelist in a XML document? (With C#) I have this xml file,I want delete the stock nodelist when an user don't have permissions to see stocklist of...
0
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes...
1
by: Que | last post by:
hi i have the following xml document <root> <item> <key> <scode></scode> </key> <scode></scode> <desc></desc> <pc></pc>
11
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.