473,320 Members | 1,722 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,320 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 12 '05 #1
3 16001
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 12 '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 12 '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 12 '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...
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...
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/>
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...
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,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.