C# 重新排列xml中的元素
我有这样的想法:C# 重新排列xml中的元素,c#,xml,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,020484
<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>
.
.
.
我如何重新排列关节所包含的儿童
以便头部
和躯干
变换位置
我一直在找,我想不出来
谢谢使用
LINQ to XML
和方法,这将起作用:
您想如何重新排列它们?您想替换
头部
和躯干
元素吗?是的,我想在列表中从头到尾都是您正在生成xml,或者只是将其作为输入接收到您的程序中?这不会用头覆盖躯干吗?@JuanCarlosAllendeMena我在这一行上得到:System.invalidoOperationException:joint.Elements(“head”).Last().replacetwith(temp)@JuanCarlosAllendeMena所有关节
元素都包含一个躯干
和一个头部
对吗?那么它就不会给你这个错误了。我尝试了你的示例xml,它工作正常
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");