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