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