Search code examples
c#asp.netlistviewxmldatasource

How to achieve reversed document ordered XmlDataSource in ListView?


I'm using an asp:XmlDataSource to list an xml document. It looks like this:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Xml/History.xml" />

And I'm using it in a asp:Listview like this:

<asp:ListView runat="server" DataSourceID="XmlDataSource1" >

I was wondering if there was a way to reverse the order, because I would like the last row of the xml to be the first row in my list. How can I achieve this?


Solution

  • if your xml looked like this (for example):

    <myxml>
      <row id="1">
      <row id="2">
    </myxml>
    

    then you can do an inline transform with something like this:

    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Xml/History.xml">
      <Transform>   
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:template match="*">
         <myxml> 
            <xsl:for-each select="//row"> 
              <xsl:sort select="@id" order="descending" /> 
              <xsl:copy-of select="row"/> 
            </xsl:for-each> 
         </myxml>
        </xsl:template>
        </xsl:stylesheet>
    </Transform>
    </asp:XmlDataSource>
    

    Hope that helps :)