开发者

add parent to xml linq

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜