473,498 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

foreach descending order

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
Nov 15 '05 #1
3 14706
"Leszek" <ta******@5thbusiness.com> wrote in
news:#v**************@TK2MSFTNGP10.phx.gbl:
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[i];
...
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
Leszek wrote:
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.
--
Girish Bharadwaj

Nov 15 '05 #3
Thanks a lot!
It was really helpful.

Leszek Taratuta

"Girish Bharadwaj" <girishb@nowhere> wrote in message
news:uq**************@TK2MSFTNGP10.phx.gbl...
Leszek wrote:
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.
--
Girish Bharadwaj

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4077
by: renster | last post by:
I have been looking at implementing the following but with a difference. http://forums.devshed.com/t47653/s.html This example uses a link which works with some code shown on the page. I have...
0
1680
by: Talis | last post by:
I have a reasonably large table (over 2 million rows) and my select results need to be sorted in descending order. I've noticed, however, that my queries ORDER'ed by DESC are at least twice as...
2
8375
by: Luigi Napolitano | last post by:
Hello, I use this algorithm to sort an array "v" in the descending order. void quickSort(float v, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) return; float mid = v;
2
1829
by: Trevor Best | last post by:
I have a unique index based on the following columns: ProjectID (int) MaterialCatalogID (int) Material catalogues are pretty much static but projects are dynamic and people are most likely to...
7
3733
by: Luigi Napolitano | last post by:
Hello, I use this algorithm to sort an array "v" in the descending order. void quickSort(float v, int lo0, int hi0) { int lo = lo0; int hi = hi0; if (lo >= hi) return; float mid = v;
32
4093
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
2
1988
by: phillip.s.powell | last post by:
SELECT s.id, s.first_name, s.last_name, IF(s.school_year_id = 0, s.school_year_other, y.school_year_alt_display) AS school_year_name FROM student s LEFT OUTER JOIN school_year y ON...
2
53996
by: Richard Hollenbeck | last post by:
I have the following query in a combo box: SELECT courses.courseCode, courses.courseDescription FROM courses ORDER BY courseDescription; I want to change the "ORDER BY" to display in...
5
7180
by: subramanian100in | last post by:
In the ISO document www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf in the detail of the qsort library function in 7.20.5.2, the description says that, the contents of the array are sorted...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.