josephdaviskcrm
asked on
ASP.NET - sort elements of an xml file with XPathNavigator class
I have the following XML file...
ÿ<?xml version="1.0" encoding="utf-8"?>
<gallery>
<album title="Landscaping" description="Images of Landscaping" lgPath="App_Photos/MainPho tos/">
<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.
ÿ<?xml version="1.0" encoding="utf-8"?>
<gallery>
<album title="Landscaping" description="Images of Landscaping" lgPath="App_Photos/MainPho
<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();
ASKER
Thats a little hard to follow. Can you give me a little more direction?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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");
http://forums.asp.net/t/961828.aspx