将XML元素移动到特定位置

时间:2016-12-12 06:49:54

标签: c# xml linq

美好的一天,我需要将所有ComposedBlock标记移动到TextBlock的最后位置,示例XML位于下方。

这是XML输入:

<parent>
<ComposedBlock ID="P1_CB0000001" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727">
    <GraphicalElement ID="P1_CB0000001_SUB" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727" />
</ComposedBlock>
<ComposedBlock ID="P1_CB0000002" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730">
    <GraphicalElement ID="P1_CB0000002_SUB" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730" />
</ComposedBlock>
<TextBlock ID="P1_TB0000006" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120" LANG="en" STYLEREFS="PAR_JUSTIFIED">
    <TextLine ID="P1_TL0000028" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120">
      <String ID="P1_ST0000165" HPOS="2539" VPOS="1680" WIDTH="63" HEIGHT="86" CONTENT="ijt" />
      <SP ID="P1_SP0000156" HPOS="2602" VPOS="1766" />
    </TextLine>
</TextBlock>
<TextBlock ID="P1_TB0000011" HPOS="4406" VPOS="5170" WIDTH="1724" HEIGHT="1003" LANG="en" STYLEREFS="PAR_JUSTIFIED">
    <TextLine ID="P1_TL0000083" HPOS="4411" VPOS="5170" WIDTH="1714" HEIGHT="120">
      <String ID="P1_ST0000532" HPOS="4411" VPOS="5170" WIDTH="346" HEIGHT="91" CONTENT="Change" />
      <SP ID="P1_SP0000513" HPOS="4757" VPOS="5261" />
      <String ID="P1_ST0000533" HPOS="4776" VPOS="5198" WIDTH="230" HEIGHT="63" CONTENT="yo0ur" />
      <SP ID="P1_SP0000514" HPOS="5006" VPOS="5261" />
    </TextLine>
</TextBlock>
</parent>

这是所需的输出:

<parent>
<TextBlock ID="P1_TB0000006" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120" LANG="en" STYLEREFS="PAR_JUSTIFIED">
    <TextLine ID="P1_TL0000028" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120">
      <String ID="P1_ST0000165" HPOS="2539" VPOS="1680" WIDTH="63" HEIGHT="86" CONTENT="ijt" />
      <SP ID="P1_SP0000156" HPOS="2602" VPOS="1766" />
    </TextLine>
</TextBlock>
<TextBlock ID="P1_TB0000011" HPOS="4406" VPOS="5170" WIDTH="1724" HEIGHT="1003" LANG="en" STYLEREFS="PAR_JUSTIFIED">
    <TextLine ID="P1_TL0000083" HPOS="4411" VPOS="5170" WIDTH="1714" HEIGHT="120">
      <String ID="P1_ST0000532" HPOS="4411" VPOS="5170" WIDTH="346" HEIGHT="91" CONTENT="Change" />
      <SP ID="P1_SP0000513" HPOS="4757" VPOS="5261" />
      <String ID="P1_ST0000533" HPOS="4776" VPOS="5198" WIDTH="230" HEIGHT="63" CONTENT="yo0ur" />
      <SP ID="P1_SP0000514" HPOS="5006" VPOS="5261" />
    </TextLine>
</TextBlock>
<ComposedBlock ID="P1_CB0000001" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727">
    <GraphicalElement ID="P1_CB0000001_SUB" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727" />
</ComposedBlock>
<ComposedBlock ID="P1_CB0000002" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730">
    <GraphicalElement ID="P1_CB0000002_SUB" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730" />
</ComposedBlock>
</parent>

这是我的代码:

XElement doc = XElement.Parse(reader);
IEnumerable<XElement> wd =
    from el in doc.Descendants("ComposedBlock").ToList()
    select el;

foreach (XElement el in wd)
{
    //what do i need to do here?
}

4 个答案:

答案 0 :(得分:2)

  • 查找当前&#34;之前的&#34;元件
  • 从树中删除
  • 在找到的元素之前插入

这样的事情:

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

答案 1 :(得分:1)

使用xml linq的另一种方法

            XElement doc = XElement.Load(FILENAME);
            XElement parent = doc.DescendantsAndSelf("parent").FirstOrDefault();
            List<XElement> wd = parent.Elements("ComposedBlock").ToList();

            foreach (XElement composedBlock in wd)
            {
                composedBlock.ReplaceWith(null);
            }
            parent.Add(wd);

答案 2 :(得分:0)

这是我的解决方案

它不是一个非常好的代码,但它可以正常工作

我创建了一个使用XElement作为数据类型的列表。我在哪里添加我需要转移的元素

def uploadedFileName = request.getFile("file")    
def fileName = uploadedFileName.originalFilename

答案 3 :(得分:0)

在VB中它看起来像这样

    For Each el As XElement In xe...<ComposedBlock>.ToList
        xe.Elements.LastOrDefault.AddAfterSelf(el)
        el.Remove()
    Next

使用它来加载和/或测试

    Dim xe As XElement
    ' to load from a file
    'xe = XElement.Load(yourpath)
    ' for testing
    xe = <parent>
             <ComposedBlock ID="P1_CB0000001" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727">
                 <GraphicalElement ID="P1_CB0000001_SUB" HPOS="6278" VPOS="1670" WIDTH="3610" HEIGHT="2727"/>
             </ComposedBlock>
             <ComposedBlock ID="P1_CB0000002" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730">
                 <GraphicalElement ID="P1_CB0000002_SUB" HPOS="653" VPOS="1728" WIDTH="1689" HEIGHT="730"/>
             </ComposedBlock>
             <TextBlock ID="P1_TB0000006" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120" LANG="en" STYLEREFS="PAR_JUSTIFIED">
                 <TextLine ID="P1_TL0000028" HPOS="2539" VPOS="1675" WIDTH="1200" HEIGHT="120">
                     <String ID="P1_ST0000165" HPOS="2539" VPOS="1680" WIDTH="63" HEIGHT="86" CONTENT="ijt"/>
                     <SP ID="P1_SP0000156" HPOS="2602" VPOS="1766"/>
                 </TextLine>
             </TextBlock>
             <TextBlock ID="P1_TB0000011" HPOS="4406" VPOS="5170" WIDTH="1724" HEIGHT="1003" LANG="en" STYLEREFS="PAR_JUSTIFIED">
                 <TextLine ID="P1_TL0000083" HPOS="4411" VPOS="5170" WIDTH="1714" HEIGHT="120">
                     <String ID="P1_ST0000532" HPOS="4411" VPOS="5170" WIDTH="346" HEIGHT="91" CONTENT="Change"/>
                     <SP ID="P1_SP0000513" HPOS="4757" VPOS="5261"/>
                     <String ID="P1_ST0000533" HPOS="4776" VPOS="5198" WIDTH="230" HEIGHT="63" CONTENT="yo0ur"/>
                     <SP ID="P1_SP0000514" HPOS="5006" VPOS="5261"/>
                 </TextLine>
             </TextBlock>
         </parent>