I want to make a deep copy of a LINQ to XML XElement. The reason I want to do this is there are some nodes in the document that I want to create modified copies of (in the same document). I don’t see a method to do this.
I could convert the element to an XML string and then reparse it, but I’m wondering if there’s a better way.
There is no need to reparse. One of the constructors of XElement takes another XElement and makes a deep copy of it:
Here are a couple of unit tests to demonstrate: