473,405 Members | 2,141 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,405 software developers and data experts.

XmlNode.ChildNodes Bug????

Hi,

I just tried to replace a single XmlNode with several ChildNodes.
When doing this with the following code i found out that if you get
the ChildNodes with 'ChildNodes' - property the nodes get removed. Is
this possibly a Framework Bug??
foreach( XmlNode node in oldNode.ChildNodes )
{
newNode.AppendChild( node );
}
parent.ReplaceChild( newNode, oldNode );
After doing several Tests is tried it the following way and it worked
fine.
[code:1:f2d0f1e03d]
for( int i = 0; i < oldNode.ChildNodes.Count; i++ )
{
XmlNode node = oldNode.ChildNodes[ i ];
newNode.AppendChild( node );
i--;
}
parent.ReplaceChild( newNode, oldNode );
[/code:1:f2d0f1e03d]

Thanks

Nov 12 '05 #1
1 3139
"ason" <sc******@inode-dot-at.no-spam.invalid> wrote in message news:41********@Usenet.com...
I just tried to replace a single XmlNode with several ChildNodes.
When doing this with the following code i found out that if you get
the ChildNodes with 'ChildNodes' - property the nodes get removed. Is
this possibly a Framework Bug??
No, it's a bug in the code example.
foreach( XmlNode node in oldNode.ChildNodes )
{
newNode.AppendChild( node );
}
parent.ReplaceChild( newNode, oldNode );
There are two important facts to realize about this code:

1. foreach uses an IEnumerator. Therefore, if you change the
collection being enumerated, you're going to invalidate the loop's
iterator and it will stop after making the first pass through the
loop's body.

2. XmlNodes have only one ParentNode. When you call AppendChild
you're changing the ParentNode of what was formerly a child node
of oldNode. This means oldNode has one less ChildNode, and new-
Node has one more (i.e., ChildNodes has changed).
After doing several Tests is tried it the following way and it worked
fine.
[code:1:f2d0f1e03d]
for( int i = 0; i < oldNode.ChildNodes.Count; i++ )
{
XmlNode node = oldNode.ChildNodes[ i ];
newNode.AppendChild( node );
i--;
}
parent.ReplaceChild( newNode, oldNode );
[/code:1:f2d0f1e03d]


Did you notice you're incrementing 'i' and then decrementing 'i'
to make this work (again, because the main factor is Append-
Child( ) alters the membership of ChildNodes)?

IEnumerator operates over an IEnumerable, which iterates in
a forward-only manner (IEnumerable has only three members:
Current, MoveNext( ) and Reset( )). As such, a foreach state-
ment can't be expected to go back/forth as you've done in the
for loop above.
Derek Harmon
Nov 12 '05 #2

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

Similar topics

2
by: Leszek | last post by:
Hello, I have the following code: // Read the XML document from the hard-drive. XmlDocument doc = new XmlDocument(); doc.Load("doc.xml"); // Create the document's root. XmlElement root =...
1
by: Venkat Venkataramanan | last post by:
Hello: Can somebody explain to me the usage of the XMLNode.ChildNodes? When there is a node that does not have any child node, the above method returns an array of 1. This member has the...
5
by: Ken Sturgeon | last post by:
Using VB.NET in VS2005, assume that you have a RegEx that matches a specific html tag; for instance "<title>.*?</title>" that returns a match "<title>SomeTitleHere</title>" and you want to convert...
1
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg.,...
0
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg.,...
2
by: Claudio | last post by:
Hi I'm trying to use XPath queries with streaming XML, but I cannot make it working. The solution I'm trying to implement is: create a XmlNode as soon as I have a full XML element and use the...
0
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
When I create a tree view control from XML document, I use XmlNode.Name in the node list iteration. But sometime, it is supposed that it got the element name. But sometimes it got the element name...
5
by: =?Utf-8?B?VGhlIE1hbiBGcm9tIFNRTA==?= | last post by:
I'm having the darndest XML config file problem that I really need help with. I'm supporting a .NET 1.1 desktop application with its own config file, and I implement IConfigurationSectionHandler...
1
by: jesper_lofgren | last post by:
Hello, i need to help here. I have a xmlnode as you can se below (full xml is in the bottom). I wonder how i can get the values inside the CSammanstallningLista . <CSammanstallning...
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
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.