foreach descending order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leszek

    #1

    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.xm l");
    XmlDocument myXmlDoc = new XmlDocument();
    myXmlDoc.Load(X ML_DOCUMENT);
    XmlElement root = myXmlDoc.Docume ntElement;
    XmlNodeList myList;
    myList = root.SelectNode s("/members/member");
    foreach (XmlNode node in myList) // this sould be descending!
    {
    ....
    }

    How to do this?
    Thanks for any hints,

    Leszek Taratuta


  • Chris R. Timmons

    #2
    Re: foreach descending order

    "Leszek" <taratuta@5thbu siness.com> wrote in
    news:#vNfyhBlDH A.1800@TK2MSFTN GP10.phx.gbl:
    [color=blue]
    > 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.xm l");
    > XmlDocument myXmlDoc = new XmlDocument();
    > myXmlDoc.Load(X ML_DOCUMENT);
    > XmlElement root = myXmlDoc.Docume ntElement;
    > XmlNodeList myList;
    > myList = root.SelectNode s("/members/member");
    > foreach (XmlNode node in myList) // this sould be descending!
    > {
    > ...
    > }
    >
    > How to do this?
    > Thanks for any hints,[/color]

    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.

    Comment

    • Girish Bharadwaj

      #3
      Re: foreach descending order

      Leszek wrote:[color=blue]
      > 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.xm l");
      > XmlDocument myXmlDoc = new XmlDocument();
      > myXmlDoc.Load(X ML_DOCUMENT);
      > XmlElement root = myXmlDoc.Docume ntElement;
      > XmlNodeList myList;
      > myList = root.SelectNode s("/members/member");
      > foreach (XmlNode node in myList) // this sould be descending!
      > {
      > ...
      > }
      >
      > How to do this?
      > Thanks for any hints,
      >
      > Leszek Taratuta
      >
      >[/color]
      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

      Comment

      • Leszek

        #4
        Re: foreach descending order

        Thanks a lot!
        It was really helpful.

        Leszek Taratuta

        "Girish Bharadwaj" <girishb@nowher e> wrote in message
        news:uqq3HjClDH A.2424@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Leszek wrote:[color=green]
        > > 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.xm l");
        > > XmlDocument myXmlDoc = new XmlDocument();
        > > myXmlDoc.Load(X ML_DOCUMENT);
        > > XmlElement root = myXmlDoc.Docume ntElement;
        > > XmlNodeList myList;
        > > myList = root.SelectNode s("/members/member");
        > > foreach (XmlNode node in myList) // this sould be descending!
        > > {
        > > ...
        > > }
        > >
        > > How to do this?
        > > Thanks for any hints,
        > >
        > > Leszek Taratuta
        > >
        > >[/color]
        > 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[/color]
        MoveNext()..[color=blue]
        >
        > 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
        >[/color]


        Comment

        Working...