ASP.NET - sort elements of an xml file with XPathNavigator class

Posted on 2009-04-21
Last Modified: 2013-11-11
I have the following XML file...

ÿ<?xml version="1.0" encoding="utf-8"?>
  <album title="Landscaping" description="Images of Landscaping" lgPath="App_Photos/MainPhotos/">
    <img src="Image1.jpg" />
    <img src="Image2.jpg" />
    <img src="Image3.jpg" />
    <img src="Image4.jpg" />
    <img src="Image5.jpg" />
    <img src="Image6.jpg" />
    <img src="Image7.jpg" />
    <img src="Image8.jpg" />

I need to be able to move to one of the img elements and move it down one passed the next one, or up one before the previous one.  What might be the best way to do this?  I have posted the code that I've got started so far, which takes me to the beginning of the first image node.
XmlDocument xmlImages = new XmlDocument();


        XPathNavigator xmlNav = xmlImages.CreateNavigator();





Open in new window

Question by:josephdaviskcrm
    LVL 12

    Expert Comment

    I found this quick example, seems to gett he basics

    Author Comment

    Thats a little hard to follow.  Can you give me a little more direction?
    LVL 12

    Accepted Solution

    actually after thinking about it, it is even easier than i was thinking.  I was thinking of how to actually sort based on an expression, but you want to manually move items up or down in the list.  The code below loads the xml file and moves index 2 (image 3) down 1 position in the list.
    XmlDocument doc = new XmlDocument();
    XmlNodeList imgNodes = doc.GetElementsByTagName("img");
    int moveIndex = 2;
    XmlNode moveNode = imgNodes[moveIndex];
    XmlNode albumNode = imgNodes[moveIndex].ParentNode;
    // for moving down one spot
    albumNode.InsertAfter(moveNode, imgNodes[moveIndex]);
    //for moving up one spot
    //albumNode.InsertBefore(moveNode, imgNodes[moveIndex]);

    Open in new window

    LVL 9

    Expert Comment

    Alternative way:

                XmlDocument doc = new XmlDocument();
                XmlNode A = doc.SelectSingleNode("//*[@src='Image3.jpg']");
                XmlNode B = A.NextSibling; // Or A.PreviousSibling;
                A.ParentNode.ReplaceChild(B.Clone(), A);
                B.ParentNode.ReplaceChild(A.Clone(), B);

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now