473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ 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="334kdns fins435" 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="334kdns fins435" 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 10433
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:

SelectSingleNod e("//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.Chil dNodes.Count)
{
if
(!peopleNode.Ch ildNodes[index].Attributes["name"].InnerText.Equa ls("tom");
peopleNode.Remo veChild(peopleN ode.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.goo glegroups.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="334kdns fins435" 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="334kdns fins435" 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:

SelectSingleNod e("//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.Chil dNodes.Count)
{
if
(!peopleNode.Ch ildNodes[index].Attributes["name"].InnerText.Equa ls("tom");
peopleNode.Remo veChild(peopleN ode.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.goo glegroups.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="334kdns fins435" 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="334kdns fins435" 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
2845
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 automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
2
2882
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 try create a param using this search critieria it can never seem to locate what I am looking for. For example, check out the following XML file:...
9
2422
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 already sucsessfully. However , the rest does not work as expected. Hope someone can help on that. Rolf
4
2328
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 the code works: I have a stack consisting of nodes and arguments, much like a function execution stack. In this example there are only three nodes...
7
3756
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
1814
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 my XML and XSLT files: XML FILE <?xml version="1.0" standalone="yes"?> <?xml-stylesheet type="text/xsl"...
1
2319
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 parent node has 100 nodes, I want to delete nodes 10 through 75, and not nodes 5, 10, 25 etc. I have a reference to the first and the last node in...
10
7152
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 have a command button, called cmdDelete, that launches a dialog form, called frmDeleteCurrentRecord, where the user has to enter their name and a...
31
6655
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 nested form. In other words, the form, m_settings_menueditor_recursive has a single subform; m_settings_menueditor_recursive (both are viewed as...
0
7895
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...
0
7826
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...
0
8182
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. ...
1
7935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8193
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...
1
5701
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...
0
3818
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...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1433
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.