473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
4 7747
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
4
by: SkyHook | last post by:
1. Under the topic "Select Nodes Using XPath Navigation" it says: "All XmlNodeList objects are synchronized with the underlying document, therefore if you ... modify the value of a node, that node...
1
by: bigeddie | last post by:
Hi, I'm trying to get an instance off a XmlNodeList without using selectNodes(...) XmlNodeList test1 = new XmlNodeList(); (C#) but that doesn't work. What's my mistake?
20
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
0
by: nandha8210 | last post by:
hi all, i want to sort an XmlNodeList based on a innertext value of a XmlNode in the XmlNodeList and would like to save the sorted order to be reflected in the source XML document from where the...
40
by: KG | last post by:
Could any one tell me how to reverse the bits in an interger?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.