Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1084407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:33:29+00:00 2026-05-16T22:33:29+00:00

I want to swap two xml elements. How do i do this? Here is

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:33:29+00:00Added an answer on May 16, 2026 at 10:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dont know why this will not work, i need to swap the two
I want to swap background images with the fade effect , I tried this:
I found this code to swap two numbers without using a third variable, using
Possible Duplicate: Swap two items in List<T> Edit: Maybe this will work for getting
I want to swap two fragment in an activity via animation.Suppose PageA is for
In most languages, if you want to swap two variables, it's something like: var
I created a custom event called slides.swap but I want to prevent the event
I want to swap the values from two rows in a table. I have
I have structure like this below. Now, I want swap 2 structures. public struct
I want to add swap functionality to two existing C++ classes. One class inherits

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.