如何使用 LINQ to XML 将 XML 元素移动到前一个元素之上?

发布于 2024-09-07 03:46:58 字数 4639 浏览 2 评论 0原文

我有以下 XML 结构:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My Program">
    <NotRoot Text="Hello">
        <SomeOption Text="Option 1" Centered="False">
            <SomeOption Text="Option 1.1" Centered="False">
                <SomeOption Text="Option 1.1.1" Centered="false">
                    <SomeOption Text="A" Centered="false">
                        <SpecialName Text="Blah blah" Centered="false">
                            <Number>1</Number>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="B" Centered="false">
                        <SpecialName Text="Hi" Centered="true">
                            <SomeStrangeName>42</SomeStrangeName>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="C" Centered="false">
                        <SpecialName Text="Some text here" Centered="false">
                            <Stuff>
                                <Thing1>10</Thing1>
                                <Thing2>20</Thing2>
                                <Thing3>30</Thing3>
                            </Stuff>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="D" Centered="false">
                        <SpecialName Text="Bye" Centered="false">
                            <Things>
                                <Random1>9846516981</Random1>
                                <Random2>8784749874</Random2>
                            </Things>
                        </SpecialName>
                    </SomeOption>
                </SomeOption>
            </SomeOption>
        </SomeOption>
    </NotRoot>
</Root>

我想将元素“C”向上移动一个位置,以便输出如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My Program">
    <NotRoot Text="Hello">
        <SomeOption Text="Option 1" Centered="False">
            <SomeOption Text="Option 1.1" Centered="False">
                <SomeOption Text="Option 1.1.1" Centered="false">
                    <SomeOption Text="A" Centered="false">
                        <SpecialName Text="Blah blah" Centered="false">
                            <Number>1</Number>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="C" Centered="false">
                        <SpecialName Text="Some text here" Centered="false">
                            <Stuff>
                                <Thing1>10</Thing1>
                                <Thing2>20</Thing2>
                                <Thing3>30</Thing3>
                            </Stuff>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="B" Centered="false">
                        <SpecialName Text="Hi" Centered="true">
                            <SomeStrangeName>42</SomeStrangeName>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="D" Centered="false">
                        <SpecialName Text="Bye" Centered="false">
                            <Things>
                                <Random1>9846516981</Random1>
                                <Random2>8784749874</Random2>
                            </Things>
                        </SpecialName>
                    </SomeOption>
                </SomeOption>
            </SomeOption>
        </SomeOption>
    </NotRoot>
</Root>

我的“向上移动元素”按钮可以使用以下代码识别要移动的元素及其上方的元素:

public void MoveElementUp(XElement xeElementToMove)
{
    XElement xeElementToMoveInXML = xmlRoot.Descendants().Single(xe => xe == xeElementToMove);
    XElement xePrevious = XElement.Parse(xeElementToMoveInXML.PreviousNode.ToString());

    // Do something here to put xeElementToMoveInXML before xePrevious

    SaveXML();
}

不过,我认为我可能会以错误的方式进行操作。也许我需要将其全部解析为常规文本,然后以某种方式移动它,然后将其转换回 XML 元素?

I have the following XML structure:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My Program">
    <NotRoot Text="Hello">
        <SomeOption Text="Option 1" Centered="False">
            <SomeOption Text="Option 1.1" Centered="False">
                <SomeOption Text="Option 1.1.1" Centered="false">
                    <SomeOption Text="A" Centered="false">
                        <SpecialName Text="Blah blah" Centered="false">
                            <Number>1</Number>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="B" Centered="false">
                        <SpecialName Text="Hi" Centered="true">
                            <SomeStrangeName>42</SomeStrangeName>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="C" Centered="false">
                        <SpecialName Text="Some text here" Centered="false">
                            <Stuff>
                                <Thing1>10</Thing1>
                                <Thing2>20</Thing2>
                                <Thing3>30</Thing3>
                            </Stuff>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="D" Centered="false">
                        <SpecialName Text="Bye" Centered="false">
                            <Things>
                                <Random1>9846516981</Random1>
                                <Random2>8784749874</Random2>
                            </Things>
                        </SpecialName>
                    </SomeOption>
                </SomeOption>
            </SomeOption>
        </SomeOption>
    </NotRoot>
</Root>

I would like to move the element "C" up one position so that the output looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="My Program">
    <NotRoot Text="Hello">
        <SomeOption Text="Option 1" Centered="False">
            <SomeOption Text="Option 1.1" Centered="False">
                <SomeOption Text="Option 1.1.1" Centered="false">
                    <SomeOption Text="A" Centered="false">
                        <SpecialName Text="Blah blah" Centered="false">
                            <Number>1</Number>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="C" Centered="false">
                        <SpecialName Text="Some text here" Centered="false">
                            <Stuff>
                                <Thing1>10</Thing1>
                                <Thing2>20</Thing2>
                                <Thing3>30</Thing3>
                            </Stuff>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="B" Centered="false">
                        <SpecialName Text="Hi" Centered="true">
                            <SomeStrangeName>42</SomeStrangeName>
                        </SpecialName>
                    </SomeOption>
                    <SomeOption Text="D" Centered="false">
                        <SpecialName Text="Bye" Centered="false">
                            <Things>
                                <Random1>9846516981</Random1>
                                <Random2>8784749874</Random2>
                            </Things>
                        </SpecialName>
                    </SomeOption>
                </SomeOption>
            </SomeOption>
        </SomeOption>
    </NotRoot>
</Root>

My "Move Element Up" button can recognize the element to move and the element above it with this code:

public void MoveElementUp(XElement xeElementToMove)
{
    XElement xeElementToMoveInXML = xmlRoot.Descendants().Single(xe => xe == xeElementToMove);
    XElement xePrevious = XElement.Parse(xeElementToMoveInXML.PreviousNode.ToString());

    // Do something here to put xeElementToMoveInXML before xePrevious

    SaveXML();
}

I think I may be going about it the wrong way though. Perhaps I need to parse it all as regular text, then move it somehow, then convert it back to XML elements?

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

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

发布评论

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

评论(1

没有你我更好 2024-09-14 03:46:58

简单:

  • 找到其当前的“上一个”元素
  • 将其从树中删除
  • 将其插入到您找到的元素之前,

如下所示:

static void MoveElementUp(XElement element)
{
    // Walk backwards until we find an element - ignore text nodes
    XNode previousNode = element.PreviousNode;
    while (previousNode != null && !(previousNode is XElement))
    {
        previousNode = previousNode.PreviousNode;
    }
    if (previousNode == null)
    {
        throw new ArgumentException("Nowhere to move element to!");
    }
    element.Remove();
    previousNode.AddBeforeSelf(element);
}

Simple:

  • Find its current "previous" element
  • Remove it from the tree
  • Insert it before the element you found

Something like this:

static void MoveElementUp(XElement element)
{
    // Walk backwards until we find an element - ignore text nodes
    XNode previousNode = element.PreviousNode;
    while (previousNode != null && !(previousNode is XElement))
    {
        previousNode = previousNode.PreviousNode;
    }
    if (previousNode == null)
    {
        throw new ArgumentException("Nowhere to move element to!");
    }
    element.Remove();
    previousNode.AddBeforeSelf(element);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文