如何在 linq to xml 中交换两个 XML 元素

发布于 2024-09-19 12:41:48 字数 1419 浏览 6 评论 0原文

我想交换两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2024-09-26 12:41:48

恐怕我还没有完全弄清楚您想要交换哪些元素,但是 XElement.ReplaceWith 就是您所追求的。这是一个简短但完整的程序来演示它:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        string xml = @"
<root>
  <element1/>
  <element2/>
  <element3/>
  <element4/>
  <element5/>
</root>";
        XDocument doc = XDocument.Parse(xml);
        XElement x = doc.Root.Element("element2");
        XElement y = doc.Root.Element("element4");
        x.ReplaceWith(y);
        y.ReplaceWith(x);
        Console.WriteLine(doc);
    }
}

它交换 element2element4

请注意,这是有效的,因为第一个 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:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        string xml = @"
<root>
  <element1/>
  <element2/>
  <element3/>
  <element4/>
  <element5/>
</root>";
        XDocument doc = XDocument.Parse(xml);
        XElement x = doc.Root.Element("element2");
        XElement y = doc.Root.Element("element4");
        x.ReplaceWith(y);
        y.ReplaceWith(x);
        Console.WriteLine(doc);
    }
}

This swaps element2 and element4.

Note that this works because the first x.ReplaceWith(y) actually creates a copy of y, leaving the original in its existing location... ready to be replaced with x.

活雷疯 2024-09-26 12:41:48

.Reverse() 以相反的顺序返回元素。它不会交换它们。

.Reverse() returns the elements in reverse order. It's doesn't swap them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文