473,473 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delete all child nodes that don't match value

Hi everyone

An easy one I think, I am just not too sure of the best way of going
about this.

I have the following XmlNode as an example:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
<person id="2" name="sue" />
<person id="3" name="jim" />
<person id="4" name="bob" />
</people>

If I wanted to chop this down to just select tom i.e.:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
</people>

How would I do this - I can't quite fathom out the technique to do it.

Many thanks

Darren

Jun 5 '06 #1
2 10424
Are you just looking to select the node, or are you trying to actually
delete all other child nodes from the XML document. I suppose your post's
title suggests the latter.

You can select the node with a simple XPath expression, such as:

SelectSingleNode("//people/person[@name='tom']");

In order to remove all other children, you first need a reference to the
parent node. Assume you have a reference to the node in your example, called
peopleNode. You could do something like this:

int index = 0;
while (index < peopleNode.ChildNodes.Count)
{
if
(!peopleNode.ChildNodes[index].Attributes["name"].InnerText.Equals("tom");
peopleNode.RemoveChild(peopleNode.ChildNodes[index]);
else
index++;
}

You could easily replace the string literal "tom" with a variable as needed.

A few things to note:

- You cannot use a foreach loop since you are modifying the child node
collection inside the loop
- Thie code assumes that each child of peopleNode does have an attribute
called 'name' (otherwise you get a null reference exception)
- This code will loop through all children even after the one you're looking
for has been found. So there's room for optimization.
- String.Equals() is case-sensitive
--
Kai Brinkmann [MSFT]
Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"daz_oldham" <Da**************@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi everyone

An easy one I think, I am just not too sure of the best way of going
about this.

I have the following XmlNode as an example:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
<person id="2" name="sue" />
<person id="3" name="jim" />
<person id="4" name="bob" />
</people>

If I wanted to chop this down to just select tom i.e.:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
</people>

How would I do this - I can't quite fathom out the technique to do it.

Many thanks

Darren

Jun 5 '06 #2
Hi Kai

Thankyou for that answer - it was spot on.

The while loop is what I needed because I am providing summary data
rather than just one specific node so it meets my needs perfectly.

Many many thanks

Darren

Kai Brinkmann [MSFT] wrote:
Are you just looking to select the node, or are you trying to actually
delete all other child nodes from the XML document. I suppose your post's
title suggests the latter.

You can select the node with a simple XPath expression, such as:

SelectSingleNode("//people/person[@name='tom']");

In order to remove all other children, you first need a reference to the
parent node. Assume you have a reference to the node in your example, called
peopleNode. You could do something like this:

int index = 0;
while (index < peopleNode.ChildNodes.Count)
{
if
(!peopleNode.ChildNodes[index].Attributes["name"].InnerText.Equals("tom");
peopleNode.RemoveChild(peopleNode.ChildNodes[index]);
else
index++;
}

You could easily replace the string literal "tom" with a variable as needed.

A few things to note:

- You cannot use a foreach loop since you are modifying the child node
collection inside the loop
- Thie code assumes that each child of peopleNode does have an attribute
called 'name' (otherwise you get a null reference exception)
- This code will loop through all children even after the one you're looking
for has been found. So there's room for optimization.
- String.Equals() is case-sensitive
--
Kai Brinkmann [MSFT]
Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"daz_oldham" <Da**************@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Hi everyone

An easy one I think, I am just not too sure of the best way of going
about this.

I have the following XmlNode as an example:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
<person id="2" name="sue" />
<person id="3" name="jim" />
<person id="4" name="bob" />
</people>

If I wanted to chop this down to just select tom i.e.:

<people group="33" status="334kdnsfins435" value"x">
<person id="1" name="tom" />
</people>

How would I do this - I can't quite fathom out the technique to do it.

Many thanks

Darren


Jun 6 '06 #3

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

Similar topics

5
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: RanDeep | last post by:
I have two nodes that both exist underneath the root node. They are linked, however, in the sense that one of the nodes contains a copy of an id that is used to refer to the other. However, when I...
9
by: Rolf Kemper | last post by:
Dear Experts, I got stuck with the following problem and need your help. What I wnat to do is to get a set of distinct nodes. Before the distinct I have selected the multiple occourences...
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...
7
by: amruta | last post by:
the code below dows not let me get the parent child view... all the nodes are show in one line only... also i need them to be collasped ... Thanks ..
1
by: renfrochris | last post by:
I'm having difficulty finding the correct syntax that will allow me to select a group of invoices based on the total of an amount column located in its line items. Below are simplified examples of...
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...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
muto222
php
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.