all groups > dotnet xml > march 2005 >
You're in the

dotnet xml

group:

sorting a xmlDocumnet


sorting a xmlDocumnet moital
3/31/2005 8:25:13 AM
dotnet xml:
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 ?
--
Re: sorting a xmlDocumnet Oleg Tkachenko
4/1/2005 3:52:32 PM
[quoted text, click to view]

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]
Re: sorting a xmlDocumnet martin
5/10/2005 6:18:03 AM
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

[quoted text, click to view]
Re: sorting a xmlDocumnet Oleg Tkachenko [MVP]
5/11/2005 12:00:00 AM
[quoted text, click to view]

XSLT can sort your document easily.

--
Oleg Tkachenko [XML MVP, MCP]
Re: sorting a xmlDocumnet martin
6/7/2005 4:21:01 AM
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

[quoted text, click to view]
AddThis Social Bookmark Button