tags:

views:

395

answers:

2

I have a DLL which I (for many reasons) cannot change. I use this assembly to retrieve a an XPathNodeIterator.

I know I can sort XML with an XPathExpression on an XPathNavigator to get an XPathNodeIterator, the problem is that I start from the XPathNodeIterator is there any way to apply a sort afterward?

A: 

Since this type is an iterator there is no way to sort the contents without enumerating the entire iterator and loading the data into a different, sortable structure.

Please see XPathNodeIterator Members for a list of the members that are available on this type (unfortunately, none of which support sorting).

Andrew Hare
yes that was what i was hoping to avoid :)
Sander
There's no performance difference. XPath queries using `XPathNavigator` obtained from `XmlDocument`, `XPathDocument`, or `XDocument` are all non-optimized, and will use linear scans. So if you do the same yourself, you're not losing anything.
Pavel Minaev
+1  A: 

XPathNodeIterator is non-generic IEnumerable of XPathNavigator. Thus, if you use .NET 3.5 and LINQ, you can do something like this:

IEnumerable<XPathNavigator> sorted =
     from XPathNavigator nav in nodeIterator
     orderby nav.GetAttribute("@id")
     select nav;
Pavel Minaev
+1 For providing a solution :)
Andrew Hare
in any case, i tested this locally and it seems to work indeed, thanksnow i hope i can use this solution on the project (depending on if i may install .net 3.5 on the testing server and if the customer is willing to install it on his server)
Sander