Friday, December 23, 2011

Appending elements while iterating an XMLNodeList

In a unit test, I had created a populated XMLNodeList and wanted to make sure that the elements were added in a certain spot within the (same parent) XMLDocument after the code under test was finished. I don't remember many of the minute details, but the code would iterate the XMLNodeList and copy the node to the document. The iteration kept failing, however.

After stepping through the code in debug mode, I found that the original XMLNodeList would get emptied out while iterating. If you want to keep the XMLNodeList intact, you need to clone the current node and push the clone into the document.

1 comment: