如何交换两个XML元素LINQ to XML中 [英] How to swap two XML elements in linq to xml

查看:136
本文介绍了如何交换两个XML元素LINQ to XML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想交换两个XML元素。我该怎么做呢?这里是代码。我试图解决的此处但它并没有为我工作毕竟。我想交换的两个元素。当我运行的元素是不能交换的程序,但是当我打电话了ToList()是交换的名单,但在商务部变量



不会被换

 <模板ID =12> 
<&标签GT;
<标签>
<名称>&TEST1 LT; /名称>
<描述/>
< /标签>
<标签>
<名称>&TEST2 LT; /名称>
<描述/>
< /标签>
< /标签>
< /模板>

下面是交换它们



<$代码p $ p> VAR DOC = XDocument.Parse(q.XMLtext);
无功电流= doc.ElementOrDefault(模板)ElementOrDefault(标签)ElementsOrDefault(标签),其中(x =方式>。(字符串)x.Element(名)==名).FirstOrDefault();
VAR上一页= Current.PreviousNode为的XElement;
VAR下一步= Current.NextNode为的XElement;
VAR CurrentName =(字符串)Current.ElementOrDefault(名称);
VAR PreviousName =(字符串)Previous.ElementOrDefault(名称);
VAR NextName =(字符串)Next.ElementOrDefault(名称);
如果(MoveDirection ==(INT)MoveType.Up)
{
doc.ElementOrDefault(模板)。ElementOrDefault(标签)。ElementsOrDefault(标签)(凡X =>(串)x.Element(名)== || CurrentName(串)x.Element(名)== PreviousName).Reverse();

}
,否则
//doc.ElementOrDefault(\"template\").ElementOrDefault(\"tabs\").ElementsOrDefault(\"tab\").Where(x => X ==电流||点¯x==下一页)。取(2).Reverse();

q.XMLtext = doc.ToString();
context.SaveChanges();


解决方案

恐怕我还没有完全制定出正是你想要交换的元素,但 的XElement .ReplaceWith 是你以后,我相信。下面是这表明它很短,但完整的程序:

 使用系统; 
使用System.Xml.Linq的;

类节目
{
静态无效的主要()
{
串XML = @
<根>
<中element1 />
<在element2 />
<元素3 />
<元素4 />
<元素5 />
< /根>中;
的XDocument DOC = XDocument.Parse(XML);
的XElement X = doc.Root.Element(element2的);
的XElement Y = doc.Root.Element(元素4);
x.ReplaceWith(Y);
y.ReplaceWith(X);
Console.WriteLine(DOC);
}
}

这互换在element2 元素4



请注意,这工作,因为第一个 X .ReplaceWith(Y)实际上创建的复制的y ,在离开现有位置原来准备...与 X


被替换

I want to swap two xml elements. How do i do this? Here is the code. I tried the solution here but it did not work for me after all. I want to swap the both elements. When I run the program the elements are not swapped but when I call ToList() it is swapped in the List but not swapped in the doc variable

<template id="12">
  <tabs>
    <tab>
      <name>test1</name>
      <description />
    </tab>
    <tab>
      <name>test2</name>
      <description />
    </tab>
  </tabs>
</template>

Here is the code to swap them

        var doc = XDocument.Parse(q.XMLtext);
        var Current = doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == name).FirstOrDefault();
        var Previous = Current.PreviousNode as XElement;
        var Next = Current.NextNode as XElement;
        var CurrentName = (string)Current.ElementOrDefault("name");
        var PreviousName = (string)Previous.ElementOrDefault("name");
        var NextName = (string)Next.ElementOrDefault("name");
        if (MoveDirection == (int)MoveType.Up)
        {
            doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => (string)x.Element("name") == CurrentName || (string)x.Element("name") == PreviousName).Reverse();

        }
        else
            //doc.ElementOrDefault("template").ElementOrDefault("tabs").ElementsOrDefault("tab").Where(x => x == Current || x == Next).Take(2).Reverse();

        q.XMLtext = doc.ToString();
        context.SaveChanges();

解决方案

I'm afraid I haven't quite worked out exactly which elements you want to swap, but XElement.ReplaceWith is what you're after, I believe. Here's a short but complete program which demonstrates it:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        string xml = @"
<root>
  <element1/>
  <element2/>
  <element3/>
  <element4/>
  <element5/>
</root>";
        XDocument doc = XDocument.Parse(xml);
        XElement x = doc.Root.Element("element2");
        XElement y = doc.Root.Element("element4");
        x.ReplaceWith(y);
        y.ReplaceWith(x);
        Console.WriteLine(doc);
    }
}

This swaps element2 and element4.

Note that this works because the first x.ReplaceWith(y) actually creates a copy of y, leaving the original in its existing location... ready to be replaced with x.

这篇关于如何交换两个XML元素LINQ to XML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆