How do I do a deep copy of an element in LINQ to XML?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In LINQ to XML, a deep copy of an XElement is easy: create a new XElement from the existing one. The constructor copies the element name, attributes, and all descendant nodes, giving you a separate XML tree that can be modified independently.
This matters because XML nodes have a parent relationship. If you try to add the same XElement instance into another location, you are moving that node, not cloning it. A constructor-based copy is the normal fix.
Create a Deep Copy with the XElement Constructor
The standard pattern is:
The copy variable now points to a new tree. Changing copy does not affect original.
Why This Counts as a Deep Copy
A deep copy duplicates:
- the element itself
- attributes
- child elements
- text nodes and other descendant nodes
That means nested XML is copied all the way down:
After this change, the original source still contains Ava, because the nested nodes were copied rather than shared.
Avoid Moving the Original by Accident
This is the mistake that usually triggers the question:
After listB.Add(item), the element is removed from listA and moved into listB. XML nodes in LINQ to XML cannot belong to two parents at once.
If you need the same structure in both places, clone it:
That leaves the original node in listA and inserts a copied version into listB.
Copy Whole Documents or Collections
The same pattern applies to XDocument:
And if you want copies of many sibling elements, project them explicitly:
This is useful when extracting parts of one document into another without disturbing the source tree.
What Does Not Come Along Automatically
The XML content is copied, but you should not think of the clone as a complete duplicate of every piece of runtime state. If your application associates extra state outside the XML tree, that is your responsibility to copy separately.
For most everyday LINQ to XML work, the constructor copy is exactly what you want because it preserves document structure and values while giving you independent nodes.
Common Pitfalls
- Adding the same
XElementto a second parent and expecting a clone. LINQ to XML moves the node instead. - Doing a constructor copy too late, after the original element has already been removed or modified elsewhere.
- Confusing a deep XML copy with copying unrelated application metadata attached outside the XML tree.
- Reusing references to child elements from the original tree after cloning. Children from the original and children from the clone are different objects.
- Copying large XML trees unnecessarily when a query over the original document would be enough.
Summary
- Use
new XElement(existingElement)to deep copy an XML element in LINQ to XML. - The copy includes attributes and all descendant nodes.
- Adding an existing node to another parent moves it instead of cloning it.
- Use the same constructor-based approach for nested elements or projected collections.
- Clone only when you need structural independence, not just read-only access.

