Reputation: 16744
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.
Upvotes: 89
Views: 26660
Reputation: 1
This should work:
var copy = new XElement(original.Name, original.Attributes(),
original.Elements() );
Upvotes: -2
Reputation: 13457
There is no need to reparse. One of the constructors of XElement takes another XElement and makes a deep copy of it:
XElement original = new XElement("original");
XElement deepCopy = new XElement(original);
Here are a couple of unit tests to demonstrate:
[TestMethod]
public void XElementShallowCopyShouldOnlyCopyReference()
{
XElement original = new XElement("original");
XElement shallowCopy = original;
shallowCopy.Name = "copy";
Assert.AreEqual("copy", original.Name);
}
[TestMethod]
public void ShouldGetXElementDeepCopyUsingConstructorArgument()
{
XElement original = new XElement("original");
XElement deepCopy = new XElement(original);
deepCopy.Name = "copy";
Assert.AreEqual("original", original.Name);
Assert.AreEqual("copy", deepCopy.Name);
}
Upvotes: 162
Reputation: 2191
Lifted directly from C# 3.0 in a Nutshell:
When a node or attribute is added to an element (whether via functional construction or an Add method) the node or attribute's Parent property is set to that element. A node can have only one parent element: if you add an already parented node to a second parent, the node is automatically deep-cloned. In the following example, each customer has a separate copy of address:
var address = new XElement ("address",
new XElement ("street", "Lawley St"),
new XElement ("town", "North Beach")
);
var customer1 = new XElement ("customer1", address);
var customer2 = new XElement ("customer2", address);
customer1.Element ("address").Element ("street").Value = "Another St";
Console.WriteLine (
customer2.Element ("address").Element ("street").Value); // Lawley St
This automatic duplication keeps X-DOM object instantiation free of side effects—another hallmark of functional programming.
Upvotes: 5
Reputation: 16744
It looks like the ToString and reparse method is the best way. Here is the code:
XElement copy = XElement.Parse(original.ToString());
Upvotes: 10
Reputation: 754893
I do not believe there is an existing mechanism that allows you to perform a deep copy of an XNode style tree. I think you are left with two options.
The visitor pattern is certainly possible but it will take a good deal of work an testing. I think your best option is #1.
Upvotes: -3