add parent to xml linq

Is it possible to add parent to xml elements ?

<node1>
 <node2></node2> 
 <node2></node2>
</node1>

and I would like to have :

<node1>
 <node3>
  <node2></node2> 
  <node2></node2>
 </node3>
</node1>

so added node3 as parent for node 2 elements in this part

Answers


XElement lastparent = node2.Parent;
node2.Remove();
node3.Add(node2);
lastparent.Add(node3);

EDIT

In order to fit your edit:

XElement node1 = yourDocument.Element("node1");
XElement[] nodes2 = node1.Elements("node2");
XElement node3 = new XElement("node3");

foreach(XElement node2 in nodes2)
{
    node2.Remove();
}

node3.Add(nodes2);
node1.Add(node3);

Need Your Help

Woocommerce WC_Order get_shipping_address() not returning as array

php wordpress woocommerce

When retrieving order information from Woocommerce it states it should be an array see..