foreach descending order

L

Leszek

Hello,

I guess it should be easy.
I need to iterate through the XML node list using foreach in descending
order:

string XML_DOCUMENT = Server.MapPath("members.xml");
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(XML_DOCUMENT);
XmlElement root = myXmlDoc.DocumentElement;
XmlNodeList myList;
myList = root.SelectNodes("/members/member");
foreach (XmlNode node in myList) // this sould be descending!
{
....
}

How to do this?
Thanks for any hints,

Leszek Taratuta
 
C

Chris R. Timmons

Hello,

I guess it should be easy.
I need to iterate through the XML node list using foreach in
descending order:

string XML_DOCUMENT = Server.MapPath("members.xml");
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(XML_DOCUMENT);
XmlElement root = myXmlDoc.DocumentElement;
XmlNodeList myList;
myList = root.SelectNodes("/members/member");
foreach (XmlNode node in myList) // this sould be descending!
{
...
}

How to do this?
Thanks for any hints,

Leszek,

You can read the list backwards by using a "for" loop instead of
"foreach". Start with the last element and iterate backwards.

XmlNode node;
for (int i = myList.Count - 1; i > -1; i--)
{
node = myList;
...
}


Hope this helps.

Chris.
 
G

Girish Bharadwaj

Leszek said:
Hello,

I guess it should be easy.
I need to iterate through the XML node list using foreach in descending
order:

string XML_DOCUMENT = Server.MapPath("members.xml");
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(XML_DOCUMENT);
XmlElement root = myXmlDoc.DocumentElement;
XmlNodeList myList;
myList = root.SelectNodes("/members/member");
foreach (XmlNode node in myList) // this sould be descending!
{
...
}

How to do this?
Thanks for any hints,

Leszek Taratuta
Foreach itself will not support ordering (unless otherwise specified. :)
). The reason is that it usually uses the IEnumerator interface exposed
by the object upon which you are iterating. While nothing stops you ,
the developer, from writing a class which gives you control .. i.e. sort
the list in a certain manner and then get the enumerator to that list,
all the existing enumerators will simply walk down the list on MoveNext()..

Thats why IEnumerator has only two methods.

In any event, if you really want that to be encapsulated, you can write
a class that would take the list, sort it in descending order and
provide an enumerator on the sorted list. Otherwise, use the for loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top