开发者

add parent to xml linq

开发者 https://www.devze.com 2023-01-06 06:32 出处:网络
Is it possible to add parent to xml elements ? <node1> <node2></node2> <node2></node2>

Is it possible to add parent to xml elements ?

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

and I wou开发者_JS百科ld like to have :

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

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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消