473,320 Members | 2,035 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.

RemoveChild question

Hi all!

I want to remove a node from a nodelist by using RemoveChild.

I'm using the following code:
XmlNode root = Xml.DocumentElement;
XmlNodeList items = root.SelectNodes("Item");
XmlNode item = items[ SomeRandomNumber ];
Root.RemoveChild(item);

The item node is removed from the XmlDocument, but it is still contained in
the items nodelist. How can I remove the node from the items nodelist?

My XML data looks like this
<Root>
<Item />
<Item />
....
Regards

Markus
Nov 12 '05 #1
5 5898
Hi Markus

I don't see the problem? The code that you've written won't actually
compile, because of the case of root/Root...but this works. I've used
GetElementsByTagName rather than SelectNodes, but it is otherwise the same.

Document Before: C:\test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Code:
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNode n = d.GetElementsByTagName("test")[2];
root.RemoveChild(n);
d.Save(@"C:\test.xml");

Document After:
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Nigel

"Markus Stehle" wrote:
Hi all!

I want to remove a node from a nodelist by using RemoveChild.

I'm using the following code:
XmlNode root = Xml.DocumentElement;
XmlNodeList items = root.SelectNodes("Item");
XmlNode item = items[ SomeRandomNumber ];
Root.RemoveChild(item);

The item node is removed from the XmlDocument, but it is still contained in
the items nodelist. How can I remove the node from the items nodelist?

My XML data looks like this
<Root>
<Item />
<Item />
....
Regards

Markus

Nov 12 '05 #2

"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com...
Hi Markus

I don't see the problem? The code that you've written won't actually
compile, because of the case of root/Root...but this works. I've used
GetElementsByTagName rather than SelectNodes, but it is otherwise the same.
Document Before: C:\test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Code:
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNode n = d.GetElementsByTagName("test")[2];
root.RemoveChild(n);
d.Save(@"C:\test.xml");

Document After:
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Nigel

Hi Nigel,

sorry, I did not describe the problem excact enough. What I want to do is to
remove the node from the nodelist (which is called 'items' in my sample).

Markus
Nov 12 '05 #3
Hi Markus

Still don't see the issue - NodeLists are live, so that as the content
changes, the NodeList changes too..., code is getting a bit unwieldy, but it
works.

Nigel

System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
MessageBox.Show(d.OuterXml);
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNodeList nl = d.GetElementsByTagName("test");
MessageBox.Show(nl.Count.ToString());
foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}
System.Xml.XmlNode n = nl[2];
root.RemoveChild(n);
MessageBox.Show(nl.Count.ToString());

foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}

"Markus Stehle" wrote:

"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com...
Hi Markus

I don't see the problem? The code that you've written won't actually
compile, because of the case of root/Root...but this works. I've used
GetElementsByTagName rather than SelectNodes, but it is otherwise the

same.

Document Before: C:\test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Code:
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNode n = d.GetElementsByTagName("test")[2];
root.RemoveChild(n);
d.Save(@"C:\test.xml");

Document After:
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>

Nigel

Hi Nigel,

sorry, I did not describe the problem excact enough. What I want to do is to
remove the node from the nodelist (which is called 'items' in my sample).

Markus

Nov 12 '05 #4
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
Hi Markus

Still don't see the issue - NodeLists are live, so that as the content
changes, the NodeList changes too..., code is getting a bit unwieldy, but it works.

Nigel

System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
MessageBox.Show(d.OuterXml);
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNodeList nl = d.GetElementsByTagName("test");
MessageBox.Show(nl.Count.ToString());
foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}
System.Xml.XmlNode n = nl[2];
root.RemoveChild(n);
MessageBox.Show(nl.Count.ToString());

foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}


Hi Nigel,

well I made some tests. If I use GetElementsByTagName() then it works - but
if I use SelectNodes() it does not work. If the nodelist was retrieved using
GetElementsByTagName() then RemoveChild() removes the child from the
nodelist. But if the list was retrieved by using SelectNodes(), calling
RemoveChild() removes the child from the xml document, but it does not
remove it from the nodelist. I'd like to use SelectNodes() in my code as I
can use XPath expressions to filter out some nodes.

Markus
Nov 12 '05 #5
Hi Markus

I've tried with SelectNodes() and see the same behaviour as you -
wierd...surely the NodeList returned in each case should be the same...would
anyone like to suggest whether this is a bug??

Nigel

"Markus Stehle" wrote:
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:02**********************************@microsof t.com...
Hi Markus

Still don't see the issue - NodeLists are live, so that as the content
changes, the NodeList changes too..., code is getting a bit unwieldy, but

it
works.

Nigel

System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
MessageBox.Show(d.OuterXml);
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNodeList nl = d.GetElementsByTagName("test");
MessageBox.Show(nl.Count.ToString());
foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}
System.Xml.XmlNode n = nl[2];
root.RemoveChild(n);
MessageBox.Show(nl.Count.ToString());

foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}


Hi Nigel,

well I made some tests. If I use GetElementsByTagName() then it works - but
if I use SelectNodes() it does not work. If the nodelist was retrieved using
GetElementsByTagName() then RemoveChild() removes the child from the
nodelist. But if the list was retrieved by using SelectNodes(), calling
RemoveChild() removes the child from the xml document, but it does not
remove it from the nodelist. I'd like to use SelectNodes() in my code as I
can use XPath expressions to filter out some nodes.

Markus

Nov 12 '05 #6

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

Similar topics

2
by: bulk88 | last post by:
How can I emulate DOM level 1 removeChild in IE 4? I already figured out to emulate getElementById by doing this if((!document.getElementById) && document.all) {document.getElementById =...
1
by: JehanNYNJ | last post by:
I need to clear out the HTML contents of a <span> element and then re-add that element as a blank span. Here is the code... var elem = document.getElementById(spanId); var deletedElem =...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
1
by: Rebecca Tsukalas | last post by:
Hello, I have a problem concerning removeChild. This is the XML structure I use with php: <xml_thing <language1 <site>bla1</site <site>bla2</site <site>bla3</site </language1
6
by: VK | last post by:
I must be missing something very obvious, but my nightly head doesn't work anymore. Press "Insert" button to add <insnodes after each <br>. Now press "Delete" - only even <insare being removed....
1
by: kwimbie | last post by:
looking for the correct code to remove the DEST_ID SUBSCR="" node with RemoveChild <WIRE_DATA> <NEED_WIRE>V</NEED_WIRE> <DEST_COUNT>2</DEST_COUNT> <DEST_ID SUBSCR="">TST</DEST_ID> <DEST_ID...
5
by: tader | last post by:
Hi, so i got script like that var div_box = document.createElement("div"); div_box.setAttribute("id", "music_box"); div_box.style.top = (Number(cords) - 10) + "px"; div_box.style.left =...
10
by: r_ahimsa_m | last post by:
Hello, On index.html page I have a table with id="property_fields". <table id="property_fields" name="property_fields" border="0"> It contains set of rows with the following IDs: var...
5
by: r_ahimsa_m | last post by:
Hello, I am lerning HTML/CSS/JavaScript. I created HTML page with table "property_fields" containing 24 rows ('tr' elements). I want to remove last 23 rows: var table =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: 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
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.