如何使用LINQ to XML将XML元素移到上一个元素的上方? [英] How can I move an XML element above the previous element using LINQ to XML?
本文介绍了如何使用LINQ to XML将XML元素移到上一个元素的上方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下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>
我的向上移动元素"按钮可以使用以下代码识别要移动的元素及其上方的元素:
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();
}
我想我可能会以错误的方式来做.也许我需要将所有内容解析为常规文本,然后以某种方式移动它,然后将其转换回XML元素?
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?
推荐答案
简单:
- 找到其当前的上一个"元素
- 将其从树上删除
- 在找到的元素之前插入
类似这样的东西:
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);
}
这篇关于如何使用LINQ to XML将XML元素移到上一个元素的上方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文