How can you sort the results of a SelectNodes result in an XMLDocument? Do I need to transform, with XSL, the document before loading it? Is that the only way? Or has anyone done this with the XPathNavigator object?
Printable View
How can you sort the results of a SelectNodes result in an XMLDocument? Do I need to transform, with XSL, the document before loading it? Is that the only way? Or has anyone done this with the XPathNavigator object?
Well I didn't see a way to do this so I learned how to do XSL Transformations, if thats what they are called. Here is what I came up with:
VB Code:
Dim ms As New IO.MemoryStream Dim sort As New Xsl.XslTransform sort.Load(Server.MapPath("\traffic.xsl")) sort.Transform(New XPathDocument(Server.MapPath("\traffic.xml")), Nothing, ms, New XmlUrlResolver) ms.Position = 0 Dim doc As New XmlDocument doc.Load(ms) ms.Close()
Here is the xsl file the attribute name is where I am sorting:
VB Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:template match="/"> <xsl:apply-templates> <xsl:sort select="@name" order="ascending" /> </xsl:apply-templates> </xsl:template> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"> <xsl:sort select="@name" order="ascending" /> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet>
Here is the xml:
VB Code:
<?xml version="1.0" standalone="yes" ?> <Traffic> <TotalHits>1525</TotalHits> <Platform name="VisualBasic6" hits="227" /> <Platform name="VisualBasicNET" hits="2709" /> <Page name="Edneeis - Control - MultiColumn ComboBox" hits="9" /> <Page name="Edneeis - Control - IconMenuExtender" hits="3" /> <Page name="Edneeis - Examples" hits="421" /> <Page name="Edneeis - Controls" hits="355" /> <Page name="Edneeis - Tutorials" hits="26" /> <Page name="Edneeis - Home" hits="1030" /> <Page name="Edneeis - Tutorial - How to pass Objects to Forms through the Constructor" hits="2" /> <Page name="Edneeis - Tutorial - How to process a Method Asynchronously with BeginInvoke" hits="5" /> <Page name="Edneeis - Tutorial - How to return Custom Objects as a Form Result" hits="3" /> <Page name="Edneeis - Tutorial - Showing Icons in Menus" hits="2" /> <Page name="Edneeis - Tutorial - Import/Export Images with a Database" hits="5" /> <Page name="Edneeis - Control - ImageComboBox" hits="2" /> <Page name="Edneeis - Control - FocusHighlighter" hits="6" /> <Page name="Edneeis - Control - Progress Panel" hits="2" /> <Page name="Edneeis - Example - Splash or Message Dialog" hits="1" /> <Page name="Edneeis - Example - Tip Of The Day" hits="1" /> </Traffic>