That's because you added it to the document.Root node:
document.Root.Add(root)

So it tacked it onto the end. That's not what you want. You need to find the correct node you want to add the new nodes to, and .add to THAT node.

-tg