将节点添加到XElement作为最后一个节点

时间:2013-12-20 10:50:42

标签: c# linq linq-to-xml

给出以下xml:

<node>
  <other/>
  <other2/>
</node>

如何使用Linq在节点结束后添加node2?

我尝试过很多没有运气的代码组合。假设这个XNode包含在节点变量中,那么对我来说最明显的是

node.LastNode.AddAfterSelf(node2);

但它给了我结果:

<node>
  <other/>
  <other2/>
</node>

我想要:

<node>
  <other/>
  <other2/>
</node>
<node2/>

最好的问候。

1 个答案:

答案 0 :(得分:0)

您可以创建新的根节点,将节点作为新根节点的子节点插入,然后将node2添加到新根节点的末尾。 然后,您需要的XML是新根的.innerXML。

相关问题