473,385 Members | 1,647 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,385 software developers and data experts.

Strange behaviour of AppendChild

Sek
Hi Folks,

I have come across a behaviour which seems strange to me.

I wrote the following code for a task:

-----BEGIN CODE-------
XmlDocument idc = new XmlDocument();
idc.LoadXml("<book><title>The Confidence
Man</title><first-name>Herman</first-name><last-name>Melville</last-name></book>");

XmlNode nd = idc.SelectSingleNode("/book");
XmlNode krs = idc.CreateElement("hello");

foreach (XmlNode cl in nd.ChildNodes)
{
Console.WriteLine(cl.Name + " : " + cl.InnerText);
krs.AppendChild(cl);
}

foreach (XmlNode ct in krs.ChildNodes)
{
Console.WriteLine(ct.Name + " : " + ct.InnerText);
}

Console.ReadLine();
return;

-----END CODE-----

I expected the code to copy the child nodes of "book" node to the
"hello" node.
Instead, only the first child of "book" was appended to "hello" node
bcoz, the foreach loop quits after first iteration.

Am i missing anything?

TIA
Sek.

Sep 19 '06 #1
1 1304


Sek wrote:

XmlDocument idc = new XmlDocument();
idc.LoadXml("<book><title>The Confidence
Man</title><first-name>Herman</first-name><last-name>Melville</last-name></book>");

XmlNode nd = idc.SelectSingleNode("/book");
XmlNode krs = idc.CreateElement("hello");

foreach (XmlNode cl in nd.ChildNodes)
{
Console.WriteLine(cl.Name + " : " + cl.InnerText);
krs.AppendChild(cl);
}
I expected the code to copy the child nodes of "book" node to the
"hello" node.
Instead, only the first child of "book" was appended to "hello" node
bcoz, the foreach loop quits after first iteration.

Using foreach over a collection that changes while you want to enumerate
is not a good idea and does not give reliable results. And the
ChildNodes collection of nd changes when you move a child cl to the krs
element with krs.AppendChild.

You can use an approach alike
while (nd.HasChildNodes) {
krs.AppendChild(nd.FirstChild);
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 19 '06 #2

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

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
9
by: Abby Lee | last post by:
http://www.ncsa.uiuc.edu/Divisions/Admin/reimb.asp There is just too much code to place here. onChange you trigger a function to add the numbers in the the column you entered an amount in...
9
by: Christian Hubinger | last post by:
Hi! I've implemented some DropDown list in ASP.NET that use Ajax to fetch the data from the server. The javascript is written to call cascading to the bottom most dropdown in order to update...
3
by: Marco Rizzi | last post by:
Hi all, i'm trying to add same node to XmlDocument. This is Xml that I want to create: <TableColumns> <TableColumn><Width>1.5in</Width></TableColumn>...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
5
by: sniper | last post by:
hi all; i'm doing here an autocompletion module in the envirnment (XFORMS/XHTML). this is the code: <?xml version="1.0" encoding="UTF-8"?> <xhtml:html xmlns="http://xforms.websynapsis.com"...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.