Question

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?

Was it helpful?

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 :)

OTHER TIPS

You could just populate a list, reverse it, then bind to that list.

EDIT: It's hard to give an example, because it'll totally depend on your XML file, but the basic idea is below, where you'd change the select statement as required and maybe add some from statements as required.

XDocument XDoc = XDocument.Load(XMLFileName);
ListView.DataSource = (from XMLNode in XDoc.Nodes()
                       select XMLNode.ToString()).ToList().Reverse();
ListView.DataBind();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top