473,462 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sorting a xmlDocumnet

i am looking for a c# way to sort an xmlDocument notes
the input of the function is a xml as string, and a path of the to be sorted
nodes
and the attribute to sort by as any one implement something like it ?
--
moital
Nov 12 '05 #1
4 3507
moital wrote:
i am looking for a c# way to sort an xmlDocument notes
the input of the function is a xml as string, and a path of the to be sorted
nodes
and the attribute to sort by as any one implement something like it ?


If you need just to select nodes and sort them, use XPathNavigator and
XPathExpression.AddSort() method. If you need to sort the whole document
(aka transform it) - XSLT is the simplest way.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2
Hi! I am looking for a similar solution. There must be some other way to sort
an Xml document! The XPathNavigator solution is bad because it returns an
XPathNodeIterator instead of an XmlDocument. Is it posible to sort the nodes
in a nodelist with the quicksort algorithm?

/marthin

"Oleg Tkachenko" wrote:
moital wrote:
i am looking for a c# way to sort an xmlDocument notes
the input of the function is a xml as string, and a path of the to be sorted
nodes
and the attribute to sort by as any one implement something like it ?


If you need just to select nodes and sort them, use XPathNavigator and
XPathExpression.AddSort() method. If you need to sort the whole document
(aka transform it) - XSLT is the simplest way.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #3
martin wrote:
Hi! I am looking for a similar solution. There must be some other way to sort
an Xml document! The XPathNavigator solution is bad because it returns an
XPathNodeIterator instead of an XmlDocument. Is it posible to sort the nodes
in a nodelist with the quicksort algorithm?


XSLT can sort your document easily.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #4
Hi again!

I've been looking on this for a while and I just don't get it. Could you
please provide example code in C# for the following:

I need a generic method for sorting Xml loaded in an XmlDocument. I want a
method like SortXml(XmlDocument doc, string swapXpath, string sortByXpath)
that returns an XmlDocument. Parameter doc is the unsorted xml loaded in an
XmlDocument, swapXpath is the xpath to the nodes that should be swapped
during sorting and the sortByXpath is the xpath to the node (element or
attribute that exists within swapXpath selection) that the swap nodes will be
sorted by.

Example:

<root>
<persons>
<person id="1">
<firstname>Foo</firstname>
<lastname>Foo</lastname>
</person>
<person id="2">
<firstname>Bar</firstname>
<lastname>Bar</lastname>
</person>
</persons>
</root>

swapXpath = root/persons/person
sortByXpath = firstname

the result should be:

<root>
<persons>
<person id="2">
<firstname>Bar</firstname>
<lastname>Bar</lastname>
</person>
<person id="1">
<firstname>Foo</firstname>
<lastname>Foo</lastname>
</person>
</persons>
</root>

This should be an "in memory operation", I don't want to read/write from/to
file/disk. I don't want a stream or a reader as return value, I want an
XmlDocument object loaded with the sorted xml as return value. How can I do
this?

Thanks!
/marthin

"Oleg Tkachenko [MVP]" wrote:
martin wrote:
Hi! I am looking for a similar solution. There must be some other way to sort
an Xml document! The XPathNavigator solution is bad because it returns an
XPathNodeIterator instead of an XmlDocument. Is it posible to sort the nodes
in a nodelist with the quicksort algorithm?


XSLT can sort your document easily.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.