locked
BindData to ListView from XML files and sorting by Date RRS feed

  • Question

  • User944339287 posted

    Hi guys.. i trying to BindData to ListView from XML files and sorting by Date but failed.

    can take a look at my code?

            Dim DS As New DataSet()
            DS.ReadXml(Server.MapPath("~/App_Data/News.xml"))
    
            If DS.Tables.Count > 0 Then
    
                Dim DR As DataRow() = DS.Tables(0).Select("type <> 'Archived' AND status = 'Active'")
    
                DS.Tables(0).DefaultView.Sort = "posted-date DESC"
                DS.AcceptChanges()
    
                If DR.Count > 0 Then
                    Me.ListView.DataSource = DR.CopyToDataTable
                    Me.ListView.DataBind()
                Else
                    Me.ListView.DataBind()
                End If
    
            Else
                Me.ListView.DataBind()
            End If



    Thursday, October 25, 2018 7:18 AM

All replies

  • User839733648 posted

    Hi kengkit,

    In my opinion, the type of posted-date in your XML is string. If you want to sort by date, you have to convert it to DateTime.

    I suggest that you could add a column to store the converted posted-date and sort by it.

    I've made as sample on my side, maybe youy could refer to it.

    .Aspx

                <asp:ListView ID="ListView1" runat="server">
                    <LayoutTemplate>
                        <table>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Age</th>
                                <th>Birth</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server"></tr>
                    </LayoutTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lblId"><%#Eval("id") %></asp:Label></td>
                            <td>
                                <asp:Label runat="server" ID="lblName"><%#Eval("name")%></asp:Label></td>
                            <td>
                                <asp:Label runat="server" ID="lblAge"><%#Eval("age") %></asp:Label></td>
                            <td>
                                <asp:Label runat="server" ID="lblBirth"><%#Eval("birth") %></asp:Label></td>
                        </tr>
                    </ItemTemplate>
                </asp:ListView>

    Code-behind.

    				Dim ds As New DataSet()
    ds.ReadXml(Server.MapPath("~/XMLFile1.xml"))
    Dim dt As DataTable = ds.Tables(0)
    dt.Columns.Add("datesort",GetType(Date))
    For Each dr As DataRow In dt.Rows
    Dim parsedDate As Date = Date.Parse(dr("birth").ToString())
    dr("datesort") = parsedDate
    Next dr
    dt.DefaultView.Sort = "datesort DESC"
    ListView1.DataSource = dt.DefaultView
    ListView1.DataBind()

    XMLFile1.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <infos>
      <info>
        <id>1</id>
        <name>aaa</name>
        <age>11</age>
        <birth>07/19/2013</birth>
      </info>
      <info>
        <id>2</id>
        <name>bbb</name>
        <age>14</age>
        <birth>07/11/2012</birth>
      </info>
      <info>
        <id>3</id>
        <name>ccc</name>
        <age>31</age>
        <birth>07/30/2018</birth>
      </info>
      <info>
        <id>4</id>
        <name>ddd</name>
        <age>18</age>
        <birth>07/19/2004</birth>
      </info>
      <info>
        <id>5</id>
        <name>eee</name>
        <age>53</age>
        <birth>09/19/2011</birth>
      </info>
    </infos>
    

    output:

    Best Regards,

    Jenifer

    Friday, October 26, 2018 8:19 AM
  • User944339287 posted

    Hi,

    What if i need to show only status = Active from xml file? TQ

    Monday, October 29, 2018 1:54 AM
  • User839733648 posted

    Hi kengkit, 

    You could modify parts of codes like below.

    My example is to select the rows of name=aaa.

    				Dim result() As DataRow = ds.Tables(0).Select("name = 'aaa'")
    				For Each dr As DataRow In result
    					Dim parsedDate As Date = Date.Parse(dr("birth").ToString())
    					dr("datesort") = parsedDate
    				Next dr
    				ListView1.DataSource = result.CopyToDataTable()
    

    XML

    <?xml version="1.0" encoding="utf-8" ?>
    <infos>
      <info>
        <id>1</id>
        <name>aaa</name>
        <age>11</age>
        <birth>07/19/2013</birth>
      </info>
      <info>
        <id>2</id>
        <name>bbb</name>
        <age>14</age>
        <birth>07/11/2012</birth>
      </info>
      <info>
        <id>3</id>
        <name>aaa</name>
        <age>31</age>
        <birth>07/30/2018</birth>
      </info>
      <info>
        <id>4</id>
        <name>ddd</name>
        <age>18</age>
        <birth>07/19/2004</birth>
      </info>
      <info>
        <id>5</id>
        <name>eee</name>
        <age>53</age>
        <birth>09/19/2011</birth>
      </info>
    </infos>
    

    Best Regards,

    Jenifer

    Monday, October 29, 2018 10:03 AM
  • User944339287 posted

    Hi,

    Below is my code. but still doesn't sort like expected. [Romantic Trip] still show on top of [Happy Trip]

            Dim DS As New DataSet()
            DS.ReadXml(Server.MapPath("~/App_Data/news.xml"))
    
            If DS.Tables.Count > 0 Then
    
                Dim results() As DataRow = DS.Tables(0).Select("[status] >= 'Active'")
                For Each dr As DataRow In results
                    Dim parsedDate As Date = Date.Parse(dr("posted-date").ToString())
                    dr("posted-date") = parsedDate
                Next dr
    
                results.CopyToDataTable.DefaultView.Sort = "posted-date DESC"
    
                Me.lvNews.DataSource = results.CopyToDataTable
                Me.lvNews.DataBind()
    
              
            Else
                Me.lvNews.DataBind()
            End If
    <?xml version="1.0" standalone="yes"?>
    <DocumentElement>
      <Details>
        <id>48112</id>
        <seo>happy-trip</seo>
        <type>text-and-images</type>
        <posted-date>2019-08-21</posted-date>
        <priority>02</priority>
        <status>Active</status>
        <name1>Happy Trip</name1>
      </Details>
      <Details>
        <id>43123</id>
        <seo>romantic-trip</seo>
        <type>text-and-images</type>
        <posted-date>2019-05-31</posted-date>
        <priority>03</priority>
        <status>Active</status>
        <name1>Romantic Trip</name1>
      </Details>
    </DocumentElement>




    Thursday, May 30, 2019 5:33 PM