[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-21
4
Medium Priority
?
339 Views
Last Modified: 2013-11-11
I have the following XML file...

ÿ<?xml version="1.0" encoding="utf-8"?>
<gallery>
  <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" />
  </album>
</gallery>

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();
        xmlImages.Load(MapPath("MainPhotos.xml"));
        XPathNavigator xmlNav = xmlImages.CreateNavigator();
        xmlNav.MoveToRoot();
        xmlNav.MoveToFirstChild();
        xmlNav.MoveToFirstChild();
        xmlNav.MoveToFirstChild();

Open in new window

0
Comment
Question by:josephdaviskcrm
  • 2
4 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24200820
I found this quick example, seems to gett he basics

http://forums.asp.net/t/961828.aspx
0
 

Author Comment

by:josephdaviskcrm
ID: 24201295
Thats a little hard to follow.  Can you give me a little more direction?
0
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 total points
ID: 24201567
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();
doc.Load("XMLFile1.xml");
XmlNodeList imgNodes = doc.GetElementsByTagName("img");
 
int moveIndex = 2;
 
XmlNode moveNode = imgNodes[moveIndex];
XmlNode albumNode = imgNodes[moveIndex].ParentNode;
albumNode.RemoveChild(moveNode);
 
// for moving down one spot
albumNode.InsertAfter(moveNode, imgNodes[moveIndex]);
 
//for moving up one spot
//albumNode.InsertBefore(moveNode, imgNodes[moveIndex]);
doc.Save("XMLFile1.xml");

Open in new window

0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24201573
Alternative way:


            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("XMLFile1.xml"));
 
            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);
            doc.Save("XMLFile1.xml");

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

831 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