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 886821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:07:59+00:00 2026-05-15T13:07:59+00:00

I have the following XML structure: <?xml version=1.0 encoding=utf-8 standalone=yes?> <Root xmlns:xsi=My Program> <NotRoot

  • 0

I have the following XML structure:

<?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>

I would like to move the element “C” up one position so that the output looks like this:

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

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?

  • 1 1 Answer
  • 2 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-15T13:08:00+00:00Added an answer on May 15, 2026 at 1:08 pm

    Simple:

    • Find its current “previous” element
    • Remove it from the tree
    • Insert it before the element you found

    Something like this:

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML structure: <?xml version=1.0 encoding=utf-8?> <xml> <root> <Item> <taxids> <string>330</string>
I have an XML file of the following structure: <?xml version=1.0 encoding=utf-8?> <root> <ArrayOfLocationDTO
I want have the following output: <?xml version=1.0 encoding=UTF-8?> <structure:structuralDataRoot xmlns:register=http://www.test.ch/register/1 xmlns:structure=http://test.ch/structure/1 > <structure:tester>ZH</structure:tester>
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8" ?> <INVOIC02> <IDOC BEGIN="1">
I have an loaded an XML document with the following structure: <?xml version=1.0 encoding=UTF-8
I have xml file whose structure is defined with following xsd: <?xml version=1.0 encoding=utf-8?>
For an XML file that has the following structure <?xml version=1.0 encoding=UTF-8> <farm xmlns:my=mynamespace.com
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8"?> <ExportData> <TransportHeader> <Timestamp>2011-01-16 06:00:33</Timestamp>
I have the following xml structure: <?xml version=1.0 encoding=utf-8 ?> <administrators> <administrator> <fullname>full name1</fullname>
Say I have this following XML structure: <?xml version=1.0 encoding=UTF-8?> <main> <parent> <child1>some value</child1>

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.