473,405 Members | 2,334 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,405 software developers and data experts.

removing nodes from xmldocument

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/>
</a>
</root>

i tried following but it does not work:

xmlNodeList l = xr.getElementsByTagName("a");

foreach(XmlNode n in l)
{
if(n.HasChildNodes == false) xr.RemoveChild(n)
}
Nov 16 '05 #1
3 3037
emid,

Try this

call this function inside the code;

void RecurssionRemoval()
{
XmlNodeList l = xr.GetElementsByTagName("a");

if(l.Count == 0)
return; //if no items exists return;

bool bItemRemoved = false;
foreach(XmlNode n in l)
{
if(n.HasChildNodes == false)
{
//go to parent node and remove the child
n.ParentNode.RemoveChild(n)
bItemRemoved = true;
break; //we have to break becoz the collection value is
changed.
}
}
if(bItemRemoved == true)
RecurssionRemoval();
}

--
Shak
(Houston)


"e-mid" <someone@somewhere> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
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/>
</a>
</root>

i tried following but it does not work:

xmlNodeList l = xr.getElementsByTagName("a");

foreach(XmlNode n in l)
{
if(n.HasChildNodes == false) xr.RemoveChild(n)
}

Nov 16 '05 #2


e-mid wrote:
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/>
</a>
</root>


The following works for me with .NET 1.1:

using System;
using System.Xml;

public class Test20040630 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040630.xml");
XmlNodeList aElements = xmlDocument.SelectNodes(@"//a[not(node())]");
Console.WriteLine(aElements.Count);
foreach (XmlNode node in aElements) {
node.ParentNode.RemoveChild(node);
}
Console.WriteLine(xmlDocument.OuterXml);
}
}

However I am not sure it is reliable to use foreach while you are
removing nodes in the collection from the document.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #3
thnkz all...
"e-mid" <someone@somewhere> wrote in message
news:u4**************@TK2MSFTNGP09.phx.gbl...
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/>
</a>
</root>

i tried following but it does not work:

xmlNodeList l = xr.getElementsByTagName("a");

foreach(XmlNode n in l)
{
if(n.HasChildNodes == false) xr.RemoveChild(n)
}

Nov 16 '05 #4

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

Similar topics

0
by: rene.rugerio[at]gmail.com | last post by:
hi forum dumb question here i have this xmldocument which is from a certain xml looping in the nodes i can see which if the attributes are empty the thing here is that i just dont know how to...
3
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/>
2
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...
11
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
3
by: Stuart Shay | last post by:
Hello All In the following XML Below which is a XMLDocument in my application. I want to remove the <extensions></extensions> Node completely. What is the best way to do this, my files are...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
2
by: RC | last post by:
Hi, 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><Apple Color =...
3
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,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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...

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.