469,282 Members | 1,921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 15820
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by rene.rugerio[at]gmail.com | last post: by
3 posts views Thread by e-mid | last post: by
11 posts views Thread by EAI | last post: by
3 posts views Thread by Stuart Shay | last post: by
1 post views Thread by Christian Rühl | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.