如何在 linq to xml 中交换两个 XML 元素
我想交换两个 xml 元素。我该怎么做?这是代码。我尝试了解决方案
<template id="12">
<tabs>
<tab>
<name>test1</name>
<description />
</tab>
<tab>
<name>test2</name>
<description />
</tab>
</tabs>
</template>
以下是交换它们的代码
var doc = XDocument.Parse(q.XMLtext);
var Current = doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == name).FirstOrDefault();
var Previous = Current.PreviousNode as XElement;
var Next = Current.NextNode as XElement;
var CurrentName = (string)Current.ElementOrDefault("name");
var PreviousName = (string)Previous.ElementOrDefault("name");
var NextName = (string)Next.ElementOrDefault("name");
if (MoveDirection == (int)MoveType.Up)
{
doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == CurrentName || (string)x.Element("name") == PreviousName).Reverse();
}
else
//doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => x == Current || x == Next).Take(2).Reverse();
q.XMLtext = doc.ToString();
context.SaveChanges();
I want to swap two xml elements. How do i do this? Here is the code. I tried the solution here but it did not work for me after all. I want to swap the both elements. When I run the program the elements are not swapped but when I call ToList() it is swapped in the List but not swapped in the doc variable
<template id="12">
<tabs>
<tab>
<name>test1</name>
<description />
</tab>
<tab>
<name>test2</name>
<description />
</tab>
</tabs>
</template>
Here is the code to swap them
var doc = XDocument.Parse(q.XMLtext);
var Current = doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == name).FirstOrDefault();
var Previous = Current.PreviousNode as XElement;
var Next = Current.NextNode as XElement;
var CurrentName = (string)Current.ElementOrDefault("name");
var PreviousName = (string)Previous.ElementOrDefault("name");
var NextName = (string)Next.ElementOrDefault("name");
if (MoveDirection == (int)MoveType.Up)
{
doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == CurrentName || (string)x.Element("name") == PreviousName).Reverse();
}
else
//doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => x == Current || x == Next).Take(2).Reverse();
q.XMLtext = doc.ToString();
context.SaveChanges();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕我还没有完全弄清楚您想要交换哪些元素,但是
XElement.ReplaceWith
就是您所追求的。这是一个简短但完整的程序来演示它:它交换
element2
和element4
。请注意,这是有效的,因为第一个
x.ReplaceWith(y)
实际上创建了y
的副本,将原始副本保留在现有位置。准备替换为x
。I'm afraid I haven't quite worked out exactly which elements you want to swap, but
XElement.ReplaceWith
is what you're after, I believe. Here's a short but complete program which demonstrates it:This swaps
element2
andelement4
.Note that this works because the first
x.ReplaceWith(y)
actually creates a copy ofy
, leaving the original in its existing location... ready to be replaced withx
..Reverse()
以相反的顺序返回元素。它不会交换它们。.Reverse()
returns the elements in reverse order. It's doesn't swap them.