XML - Clone() & InsertAfter() doubles node?


Anyone have any idea why my code clones a node and then writes it into my document at the specified location twice? Does this sound familiar to anyone?

XmlNode newItem = tempRoot.ChildNodes[0].Clone();
tempRoot.InsertAfter(newItem, tempRoot.ChildNodes[navNum]);

I have really isolated this problem and feel very certain that the problem arises from how I am using InsertAfter.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

AvonWyssConnect With a Mentor Commented:
Well, your source code suggests otherwise! You're cloning one of tempRoot's childs, and add it as one of tempRoot's childs. Thats seems like inserting the clone just where the original node already was, doesn't it?
Well, is the node which you are cloning maybe already there where you insert the clone?
coltrane2003Author Commented:
Yes I am sure it is not already there.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

coltrane2003Author Commented:
Yes I see your point, but I have taken this into account. I should have clarified this part. I was ending up with 3 nodes that look like that instead of two - if that makes any sense.

My plan was to clone the first node and then to modify some of its values rather than to build that same node from scratch. This was advantageous because there are some cases where I'm not sure what structure the node will have.

In any case it is now mysteriously working! I've been stuck for 2 hours and now I can't repro! Very annoying.

Thanks for having a look.  
You're welcome!

But frankly, I don't think a C grade was appropriate here; may I kindly ask you to read the EE guidelines on grading for the next time? Thank you, and have a nice weekend.
coltrane2003Author Commented:
OK I'm sorry. You didn't actually solve my specific problem and so I didn't know what to do. I just gave you a C without thinking about it too much. I hadn't read the grading policy.

After reflecting on the answer you gave, I see that it is the best possible answer that you could have given based on the information you had. I will request that a moderator change the grade.

Thanks again.
Since I saw in your profile that you are a newcomer here, I guessed that you may not yet know that policy so well. Don't worry about it, and welcome to EE!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.