1

I have some Xml that I am trying to change the name of the XElement on. The XElement contains both child elements and a value similar to this

<parent>
  Hi I am a value <a href="link">link</a>
</parent>

I tried using the method suggested here but i can only copy either the elements or the value. Setting the elements first and then the value wipes out the elements.

<newparent>
  Hi I am a value link
</newparent>

Copying the value and then elements duplicates the element text

<newparent>
  Hi I am a value link <a href="link">link</a>
</newparent>

String manipulation of the value itself escapes all of the brackets.

<newparent>
  Hi I am a value link &lt;a href="link"&gt;link&lt;/a&gt;
</newparent>

Does someone know of a way to achieve the result of copying the value and elements over to the newly created element correctly?

Community
  • 1
  • 1
jamesamuir
  • 1,397
  • 3
  • 19
  • 41

1 Answers1

1

If I get your question correctly, you want all the child nodes copied to a new parent with a different name. If so, use the Nodes() method.

XElement parent = XElement.Parse("<parent>Hi I am a value <a href=\"link\">link</a></parent>");
var newparent = new XElement("newparent", parent.Nodes());
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69