Friday, November 21, 2008

HowTo: XmlNode, Clone and InsertBefore

If we have a XmlDocument named "doc" and you want to Clone and Insert the Clone before the original:

// Select your XmlNode
XmlNode myOriginal = doc.FirstChild; // Use the code to select your XmlNode

// Clone the XmlNode
XmlNode myClone = myOriginal.Clone();

// Insert the clone before the original
myOriginal .ParentNode.InsertBefore(myClone , myOriginal );

// The XmlNode you call InsertBefore from must be parent to the parameter refChild.

No comments: