You've posted on the wrong forum - this area is for XSLT questions, whereas your problem is related to DOM.
I'm no expert on DOM but I think that if you do
a.InsertBefore(b ,c)
then b must be a child of a, and the message is telling you that it isn't (it's probably the same node as a)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference