473,379 Members | 1,216 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,379 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 16005
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,...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.