Alex Rivera | Logout

How do I do a deep copy of an element in LINQ to XML?

Asked 2008-10-16T17:52:02.153
90

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.

Edit
Report

1 Answer

-2

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.

  1. Do as you suggested an convert to a string and then back into a tree
  2. Write on yourself with a visitor pattern

The visitor pattern is certainly possible but it will take a good deal of work an testing. I think your best option is #1.

answered 2008-10-16T17:58:21.517

Your Answer