Wednesday 20 November 2013

C# Tips : Sort or order by Date in XML File data using LINQ with C#.Net and VB.Net example


 XML file contains various types of data like string, integer, date, etc. Now if we want to get data from XML file order by some attribute which contains dates at that time we need to do some special things if we are not doing that at that time data is not sorted date wise it will treat as normal string sorting. We can do date wise sorting. 

Here are examples for this.
In this example we are taking on 'Books.xml' file which contains books details in this XML file for each book element we are one "ReleaseDate" element which contains date value. Now we take that XML file in XMLDocument object and sort on "ReleaseDate" attribute by converting that string date into date datatype.


C#. Net Example :
        XDocument doc = XDocument.Load((Server.MapPath("Books.xml")));
        var query =
            from objBooks in doc.Descendants("Book")
            let RelaseDate = (DateTime)objBooks.Element("ReleaseDate")
            orderby RelaseDate
            select objBooks;

        foreach (var objBook in query.ToList())
        {
            Response.Write("<b>Book Title : </b>" + objBook.Element("Title").Value );
            Response.Write("<br/>");
            Response.Write("<b>Author Name : </b>" + objBook.Element("Author").Element("FirstName").Value);
            Response.Write("<br/>");
            Response.Write("<b>Release Date : </b>" + objBook.Element("ReleaseDate").Value);
            Response.Write("<br/>===============================================<br/>");
        }

VB.Net Examples :
        Dim doc As XDocument = XDocument.Load((Server.MapPath("Books.xml")))
        Dim query = From objBooks In doc.Descendants("Book")
                    Let RelaseDate = CType(objBooks.Element("ReleaseDate"), Date) Order By RelaseDate Select objBooks

        For Each objBook In query.ToList()
            Response.Write("<b>Book Title : </b>" + objBook.Element("Title").Value)
            Response.Write("<br/>")
            Response.Write("<b>Author Name : </b>" + objBook.Element("Author").Element("FirstName").Value)
            Response.Write("<br/>")
            Response.Write("<b>Release Date : </b>" + objBook.Element("ReleaseDate").Value)
            Response.Write("<br/>===============================================<br/>")
        Next

Books.xml File : 
<?xml version="1.0" encoding="utf-8" ?>
<Books>
    <Book >
        <Title>ASP.NET</Title>
        <ISBN>asp1</ISBN>
        <ReleaseDate>11/11/2010</ReleaseDate>
        <Pages>200</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>Pratik</FirstName>
            <LastName>Patel</LastName>
        </Author>
    </Book>
    <Book>
        <Title>C#.NET</Title>
        <ISBN>c#2</ISBN>
        <ReleaseDate>10/11/2010</ReleaseDate>
        <Pages>500</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>David</FirstName>
            <LastName>Anderson</LastName>
        </Author>
    </Book>
    <Book>
        <Title>VB.NET</Title>
        <ISBN>vb3</ISBN>
        <ReleaseDate>5/5/2009</ReleaseDate>
        <Pages>400</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>Roger</FirstName>
            <LastName>Z.</LastName>
        </Author>
    </Book>
    <Book>
        <Title>SQL Server</Title>
        <ISBN>sql4</ISBN>
        <ReleaseDate>6/9/2010</ReleaseDate>
        <Pages>300</Pages>
        <PublisherId>2</PublisherId>
        <Author>
            <FirstName>Pratik</FirstName>
            <LastName>Patel</LastName>
        </Author>
    </Book>
    <Book>
        <Title>JAVA</Title>
        <ISBN>java5</ISBN>
        <ReleaseDate>8/5/2011</ReleaseDate>
        <Pages>400</Pages>
        <PublisherId>3</PublisherId>
        <Author>
            <FirstName>David</FirstName>
            <LastName>Anderson</LastName>
        </Author>
    </Book>
    <Book>
        <Title>HTML</Title>
        <ISBN>html6</ISBN>
        <ReleaseDate>9/5/2011</ReleaseDate>
        <Pages>400</Pages>
        <Author>
            <FirstName>Roger</FirstName>
            <LastName>Z.</LastName>
        </Author>
    </Book>
</Books>

Output :


Below are the books that you would like :

No comments:

Post a Comment