gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:32 28 4
gpt4 key购买 nike

我有以下 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 元素?

最佳答案

简单:

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

像这样:

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);
}

关于c# - 如何使用 LINQ to XML 将 XML 元素移动到前一个元素之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3087891/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com