467,147 Members | 1,275 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,147 developers. It's quick & easy.

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?

Oct 31 '06 #1
  • viewed: 7127
Share:
4 Replies
You could try something like

List<XmlNodechildren = new List<XmlNode>();
foreach (XmlNode child in myNode.ChildNodes)
{
children.Add(myNode.Remove(child));
}
for (int i = children.Count - 1; i >= 0; i--)
{
myNode.AppendChild(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

"eg****@gmail.com" 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?

Oct 31 '06 #2
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(Count - (index + 1));
}
public override System.Collections.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.DocumentElement.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);
}
Oct 31 '06 #3
Hi,

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

On Oct 31, 4:00 am, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.comwrot e:
You could try something like

List<XmlNodechildren = new List<XmlNode>();
foreach (XmlNode child in myNode.ChildNodes)
{
children.Add(myNode.Remove(child));}for (int i = children.Count - 1; i >= 0; i--)
{
myNode.AppendChild(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.com" 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?
Nov 2 '06 #4
Thanks your class works great!

On Oct 31, 4:19 am, "Marc Gravell" <marc.grav...@gmail.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(Count - (index + 1));
}
public override System.Collections.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.DocumentElement.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);
}
Nov 2 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
3 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
14 posts views Thread by ford_desperado@yahoo.com | last post: by
10 posts views Thread by aatish19@gmail.com | last post: by
1 post views Thread by bigeddie | last post: by
20 posts views Thread by mike7411@gmail.com | last post: by
40 posts views Thread by KG | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.