Wednesday, 30 May 2012

C# Programming : Querying the XML data file using LINQ in LINQ to XML

Using LINQ to XML we can use the same basic LINQ syntax to query XML documents.
LINQ to XML uses System.Xml.Linq namespace.
Here are example for this.
In this example we have Books.xml file which contains books details. Now we query this xml files and display it's data in grid using LINQ .
In LINQ to XML we have to provide mapping logic in LINQ query.

Here are examples :

XML File : (Books.xml)
<?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>
  </Book>
  <Book>
    <Title>C#.NET</Title>
    <ISBN>c#2</ISBN>
    <ReleaseDate>10/11/2010</ReleaseDate>
    <Pages>500</Pages>
    <PublisherId>1</PublisherId>
  </Book>
  <Book>
    <Title>VB.NET</Title>
    <ISBN>vb3</ISBN>
    <ReleaseDate>5/5/2009</ReleaseDate>
    <Pages>400</Pages>
    <PublisherId>1</PublisherId>
  </Book>
  <Book>
    <Title>SQL Server</Title>
    <ISBN>sql4</ISBN>
    <ReleaseDate>6/9/2010</ReleaseDate>
    <Pages>300</Pages>
    <PublisherId>2</PublisherId>
  </Book>
  <Book>
    <Title>JAVA</Title>
    <ISBN>java5</ISBN>
    <ReleaseDate>8/5/2011</ReleaseDate>
    <Pages>400</Pages>
    <PublisherId>3</PublisherId>
  </Book>
</Books>

ASPX Code :
<form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvBooks" runat="server">
        </asp:GridView>
    </div>
</form>

C# Example :
 public class Books
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }
        public int PublisherId { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var query = from m in
                    XElement.Load(MapPath("Books.xml")).Elements("Book")
                    select new Books
                    {
                        Title = (string)m.Element("Title"),
                        ISBN = (string)m.Element("ISBN"),
                        ReleaseDate = (DateTime)m.Element("ReleaseDate"),
                        Pages = (int)m.Element("Pages"),
                        PublisherId = (int)m.Element("PublisherId")
                    };

        gvBooks.DataSource = query;
        gvBooks.DataBind();
    }

VB.net Example :
 Public Class Books
        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
        Public Property PublisherId As Integer
    End Class

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim query = From m In XElement.Load(MapPath("Books.xml")).Elements("Book") _
                    Select New Books With { _
                        .Title = CStr(m.Element("Title")), _
                        .ISBN = CStr(m.Element("ISBN")), _
                        .ReleaseDate = CDate(m.Element("ReleaseDate")), _
                        .Pages = CInt(m.Element("Pages")), _
                        .PublisherId = CInt(m.Element("PublisherId")) _
                    }
        gvBooks.DataSource = query
        gvBooks.DataBind()
    End Sub

Output : 


For Beginning .Net articles. Click Here...

This type of .Net Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.




 

1 comment: