LINQ to XML:上下移动节点的最有效方法是什么

发布于 2024-09-19 18:13:47 字数 462 浏览 3 评论 0原文

我需要在某些节点之前和之后移动同级节点。这是我正在使用的代码,

<tabs>
     <tab>
          <name>Overview</name>
     </tab>
     <tab>
          <name>Testing</name>
     </tab>
     <tab>
          <name>Performance</name>
     </tab>
     <tab>
          <name>Braking</name>
     </tab>
</tabs>

我想将带有测试的选项卡移至概述上方。我将如何使用 linq to XML 来解决这个问题?

I need to move sibling nodes before and after certain nodes. Here is the code im working with

<tabs>
     <tab>
          <name>Overview</name>
     </tab>
     <tab>
          <name>Testing</name>
     </tab>
     <tab>
          <name>Performance</name>
     </tab>
     <tab>
          <name>Braking</name>
     </tab>
</tabs>

I would like to move the tab with testing in it above Overview. How would I go about this using linq to XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

不及他 2024-09-26 18:13:48

您可以使用以下内容:

    var tests = from node in doc.Descendants("name") where node.Value == "Testing" select node;
    var test = tests.Single();
    var tab = test.Parent;
    var tabs = tab.Parent;
    tab.Remove();
    tabs.AddFirst(tab);

不确定您的 XML 结构有多少是固定的/已知的。

You can use something like:

    var tests = from node in doc.Descendants("name") where node.Value == "Testing" select node;
    var test = tests.Single();
    var tab = test.Parent;
    var tabs = tab.Parent;
    tab.Remove();
    tabs.AddFirst(tab);

Not sure how much of your XML structure is fixed / known.

战皆罪 2024-09-26 18:13:48

我知道这篇文章很旧,但我今天遇到了同样的问题,最终以这种方式解决:

private void SwapXNodes(bool up, int inUniqueID)
    {
        XElement currNode = DocumentManager.xMainDocument.XPathSelectElement("//*[@UniqueID='" + inUniqueID + "']"); // find 

        if (up)
        {
            if (currNode.PreviousNode != null)
            {
                XElement xPrevious = new XElement((XElement)currNode.PreviousNode); // copy of previous node

                currNode.PreviousNode.ReplaceWith(currNode); // previous node equal to me
                currNode.ReplaceWith(xPrevious); // Now I should be equal to previous node
            }
        }
        else
        {
            if (currNode.NextNode != null)
            {
                XElement xNext = new XElement((XElement)currNode.NextNode); // copy of Next node

                currNode.NextNode.ReplaceWith(currNode); // Next node equal to me
                currNode.ReplaceWith(xNext); // Now I should be equal to Next node copy
            }
        }
    }

I know this post is old, but I came up with the same issue today and end up resolving it this way:

private void SwapXNodes(bool up, int inUniqueID)
    {
        XElement currNode = DocumentManager.xMainDocument.XPathSelectElement("//*[@UniqueID='" + inUniqueID + "']"); // find 

        if (up)
        {
            if (currNode.PreviousNode != null)
            {
                XElement xPrevious = new XElement((XElement)currNode.PreviousNode); // copy of previous node

                currNode.PreviousNode.ReplaceWith(currNode); // previous node equal to me
                currNode.ReplaceWith(xPrevious); // Now I should be equal to previous node
            }
        }
        else
        {
            if (currNode.NextNode != null)
            {
                XElement xNext = new XElement((XElement)currNode.NextNode); // copy of Next node

                currNode.NextNode.ReplaceWith(currNode); // Next node equal to me
                currNode.ReplaceWith(xNext); // Now I should be equal to Next node copy
            }
        }
    }
阳光下的泡沫是彩色的 2024-09-26 18:13:47

您可以通过删除元素然后将它们重新插入到所需的位置来移动元素:

var doc = XDocument.Parse(@"<tabs>...</tabs>");

var tab = doc.Root.Elements().ElementAt(1);
tab.Remove();
doc.Root.AddFirst(tab);

或者,您可以按照所需的顺序从现有元素创建一个新文档:

var doc = XDocument.Parse(@"<tabs>...</tabs>");

var tabs = doc.Root.Elements();

var result = new XDocument(
                 new XElement("tabs", 
                     tabs.ElementAt(1),
                     tabs.ElementAt(0),
                     tabs.ElementAt(2)));

我还没有测试过它,但这可能有效:

void Swap(XElement a, XElement b)
{
    var c = new XElement("dummy");
    a.ReplaceWith(c);
    b.ReplaceWith(a);
    c.ReplaceWith(b);
}

You can move the elements by removing them and then reinserting them at the desired position:

var doc = XDocument.Parse(@"<tabs>...</tabs>");

var tab = doc.Root.Elements().ElementAt(1);
tab.Remove();
doc.Root.AddFirst(tab);

Alternatively, you can create a new document from the existing elements in the desired order:

var doc = XDocument.Parse(@"<tabs>...</tabs>");

var tabs = doc.Root.Elements();

var result = new XDocument(
                 new XElement("tabs", 
                     tabs.ElementAt(1),
                     tabs.ElementAt(0),
                     tabs.ElementAt(2)));

I haven't tested it, but this might work:

void Swap(XElement a, XElement b)
{
    var c = new XElement("dummy");
    a.ReplaceWith(c);
    b.ReplaceWith(a);
    c.ReplaceWith(b);
}
垂暮老矣 2024-09-26 18:13:47

抱歉,这是 VB.NET 和 XML Literals,但它可以用 C# 来完成。这里的想法是使用 Reverse 扩展方法:

Sub Main()
        Dim tab = <tabs>
                      <tab>
                          <name>Overview</name>
                      </tab>
                      <tab>
                          <name>Testing</name>
                      </tab>
                      <tab>
                          <name>Performance</name>
                      </tab>
                      <tab>
                          <name>Braking</name>
                      </tab>
                  </tabs>
        Console.WriteLine(SwapElements("Testing", "Performance", tab).ToString)
        Console.ReadLine()
    End Sub
    Function SwapElements(ByVal firstElement As String, ByVal secondElement As String, ByVal tab As XElement) As XElement
        Dim swapped = tab.Elements.Where(Function(e) e.Value = firstElement Or e.Value = secondElement).Reverse
        Dim middle = tab.Elements.SelectMany(Function(e) e.ElementsAfterSelf.Where(Function(f) e.Value = firstElement).TakeWhile(Function(g) g.Value <> secondElement))
        swapped.ElementAt(0).AddAfterSelf(middle)
        Return <<%= tab.Name %>>
                   <%= tab.Elements.Select(Function(e) e.ElementsBeforeSelf.Where(Function(f) e.Value = firstElement)) %>
                   <%= swapped %>
                   <%= tab.Elements.Select(Function(e) e.ElementsAfterSelf.Where(Function(f) e.Value = secondElement)) %>
               </>
    End Function

Sorry, this is VB.NET and XML Literals, but it can be done old school in C#. The idea here is to use the Reverse extention method:

Sub Main()
        Dim tab = <tabs>
                      <tab>
                          <name>Overview</name>
                      </tab>
                      <tab>
                          <name>Testing</name>
                      </tab>
                      <tab>
                          <name>Performance</name>
                      </tab>
                      <tab>
                          <name>Braking</name>
                      </tab>
                  </tabs>
        Console.WriteLine(SwapElements("Testing", "Performance", tab).ToString)
        Console.ReadLine()
    End Sub
    Function SwapElements(ByVal firstElement As String, ByVal secondElement As String, ByVal tab As XElement) As XElement
        Dim swapped = tab.Elements.Where(Function(e) e.Value = firstElement Or e.Value = secondElement).Reverse
        Dim middle = tab.Elements.SelectMany(Function(e) e.ElementsAfterSelf.Where(Function(f) e.Value = firstElement).TakeWhile(Function(g) g.Value <> secondElement))
        swapped.ElementAt(0).AddAfterSelf(middle)
        Return <<%= tab.Name %>>
                   <%= tab.Elements.Select(Function(e) e.ElementsBeforeSelf.Where(Function(f) e.Value = firstElement)) %>
                   <%= swapped %>
                   <%= tab.Elements.Select(Function(e) e.ElementsAfterSelf.Where(Function(f) e.Value = secondElement)) %>
               </>
    End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文