重新排列xml中的元素

时间:2014-03-04 17:44:08

标签: c# xml

我有这样的事情:

<MotionCapture height="480" dialogueFile="none" width="640" fps="30" numFrames="55" depth="200.0">
  <MocapFrame index="0">
    <Skeleton id="0">
      <Joints>
        <torso x="0,09970227" z="2,13386" y="0,02048468" />
        <neck x="0,139027" z="2,11847" y="0,3753783" />
        <head x="0,1632785" z="2,102617" y="0,5890977" />
        <l_shoulder x="-0,02443917" z="2,124225" y="0,2805836" />
        <r_shoulder x="0,301287" z="2,170277" y="0,2579407" />
        <l_hip x="0,01964889" z="2,067304" y="-0,1138878" />
        <l_knee x="-0,05473036" z="1,961696" y="-0,4850187" />
        <l_foot x="-0,1081518" z="1,841429" y="-0,7449498" />
        <r_hip x="0,1662257" z="2,089662" y="-0,1240771" />
        <r_knee x="0,1636017" z="2,024291" y="-0,5009199" />
        <r_foot x="0,1338794" z="1,972047" y="-0,8106034" />
      </Joints>
    </Skeleton>
  </MocapFrame>
.
.
.
</MotionCapture>

如何重新排列Joints所包含的子项,以便headtorso更改位置?

我一直在寻找,我无法理解。

谢谢

1 个答案:

答案 0 :(得分:1)

这可行,使用LINQ to XMLXElement.ReplaceWith方法:

var xmlDocument = XDocument.Load("path");
var Joints = xmlDocument.Descendants("Joints");

foreach (var joint in Joints)
{
    var temp = joint.Element("torso");
    joint.Element("torso").ReplaceWith(joint.Element("head"));
    joint.Elements("head").Last().ReplaceWith(temp);
}    
xmlDocument.Save("path");
相关问题