Reverse XmlNodeList Order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eggie5@gmail.com

    #1

    Reverse XmlNodeList Order

    Hi,

    I have an XmlNodeList and I need to reverse it. Just like
    Array.Reverse() , but it has to stay as an XmlNodeList.

    Any ideas?

  • Sergey Poberezovskiy

    #2
    RE: Reverse XmlNodeList Order

    You could try something like

    List<XmlNodechi ldren = new List<XmlNode>() ;
    foreach (XmlNode child in myNode.ChildNod es)
    {
    children.Add(my Node.Remove(chi ld));
    }
    for (int i = children.Count - 1; i >= 0; i--)
    {
    myNode.AppendCh ild(children[i]);
    }

    It may not be the fastest, but it is a way. I have not tested it, but it
    should be pretty close to the real thing :-)

    HTH

    "eggie5@gmail.c om" wrote:
    Hi,
    >
    I have an XmlNodeList and I need to reverse it. Just like
    Array.Reverse() , but it has to stay as an XmlNodeList.
    >
    Any ideas?
    >
    >

    Comment

    • Marc Gravell

      #3
      Re: Reverse XmlNodeList Order

      OK... this is downright dirty, but it seems to work...

      It does at least mean that it stays in sync with the source document (if
      that respects updates... not sure)...

      Marc

      public class ReverseXmlList : XmlNodeList {
      private readonly XmlNodeList _source;
      public ReverseXmlList( XmlNodeList source)
      {
      _source = source;
      }
      public override XmlNode Item(int index)
      {
      return _source.Item(Co unt - (index + 1));
      }
      public override System.Collecti ons.IEnumerator GetEnumerator()
      {
      for (int i = Count - 1; i >= 0; i--)
      {
      yield return _source.Item(i) ;
      }
      }
      public override int Count
      {
      get { return _source.Count; }
      }
      }
      static void Main(string[] args)
      {
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(@"< Xml><A/><B/><C/></Xml>");
      XmlNodeList original = doc.DocumentEle ment.ChildNodes ;
      XmlNodeList reverse = new ReverseXmlList( original);
      foreach (XmlNode node in reverse)
      {
      Debug.WriteLine (node.Name);
      }
      Debug.WriteLine (reverse[0].Name);
      Debug.WriteLine (reverse.Item(0 ).Name);
      }


      Comment

      • eggie5

        #4
        Re: Reverse XmlNodeList Order

        Hi,

        What is myNode in your example? I need a reversed XmlNodeList to be
        returned.

        On Oct 31, 4:00 am, Sergey Poberezovskiy
        <SergeyPoberezo vs...@discussio ns.microsoft.co mwrote:
        You could try something like
        >
        List<XmlNodechi ldren = new List<XmlNode>() ;
        foreach (XmlNode child in myNode.ChildNod es)
        {
        children.Add(my Node.Remove(chi ld));}for (int i = children.Count - 1; i >= 0; i--)
        {
        myNode.AppendCh ild(children[i]);
        >
        }It may not be the fastest, but it is a way. I have not tested it, but it
        should be pretty close to the real thing :-)
        >
        HTH
        >
        "egg...@gmail.c om" wrote:
        Hi,
        >
        I have an XmlNodeList and I need to reverse it. Just like
        Array.Reverse() , but it has to stay as an XmlNodeList.
        >
        Any ideas?

        Comment

        • eggie5

          #5
          Re: Reverse XmlNodeList Order

          Thanks your class works great!

          On Oct 31, 4:19 am, "Marc Gravell" <marc.grav...@g mail.comwrote:
          OK... this is downright dirty, but it seems to work...
          >
          It does at least mean that it stays in sync with the source document (if
          that respects updates... not sure)...
          >
          Marc
          >
          public class ReverseXmlList : XmlNodeList {
          private readonly XmlNodeList _source;
          public ReverseXmlList( XmlNodeList source)
          {
          _source = source;
          }
          public override XmlNode Item(int index)
          {
          return _source.Item(Co unt - (index + 1));
          }
          public override System.Collecti ons.IEnumerator GetEnumerator()
          {
          for (int i = Count - 1; i >= 0; i--)
          {
          yield return _source.Item(i) ;
          }
          }
          public override int Count
          {
          get { return _source.Count; }
          }
          }
          static void Main(string[] args)
          {
          XmlDocument doc = new XmlDocument();
          doc.LoadXml(@"< Xml><A/><B/><C/></Xml>");
          XmlNodeList original = doc.DocumentEle ment.ChildNodes ;
          XmlNodeList reverse = new ReverseXmlList( original);
          foreach (XmlNode node in reverse)
          {
          Debug.WriteLine (node.Name);
          }
          Debug.WriteLine (reverse[0].Name);
          Debug.WriteLine (reverse.Item(0 ).Name);
          }

          Comment

          Working...