connectseekers.com

Saturday, 11 October 2014

Read XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net Using C#.Net

10/11/2014 - By Pranav Singh 0

This article will show you how you can access read xml file data using linq query and add in datatable to bind gridview in asp.net using c#.net. This dataset we will bind with the gridview using c#.net.


So for this article first we will create a new asp.net application and add the below code into .aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Read XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net Using C#.Net
    </title>
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" Width="100%">
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

After this we will create an xml file in below shown format.

xml version="1.0" encoding="utf-8" ?>
<CountryModel>
  <Country>
    <ID>1</ID>
    <COUNTRY>India</COUNTRY>
    <POPULATION>20000</POPULATION>
  </Country>
  <Country>
    <ID>2</ID>
    <COUNTRY>Pakistan</COUNTRY>
    <POPULATION>30000</POPULATION>
  </Country>
  <Country>
    <ID>3</ID>
    <COUNTRY>Sri Lanka</COUNTRY>
    <POPULATION>40000</POPULATION>
  </Country>
  <Country>
    <ID>4</ID>
    <COUNTRY>Nepal</COUNTRY>
    <POPULATION>50000</POPULATION>
  </Country>
  <Country>
    <ID>5</ID>
    <COUNTRY>Bhutan</COUNTRY>
    <POPULATION>60000</POPULATION>
  </Country>
</CountryModel>

In above code I have added a gridview control and I will bind it with xml data. Now add the below code into your .cs page.

using System;
using System.Linq;
using System.Data;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
            }
        }
        public void GetData()
        {
            try
            {
                XDocument doc = XDocument.Load(Server.MapPath("~/XML/XMLFile1.xml"));
                var records = (from data in doc.Root.Elements("Country")
                               select data);
                if (records != null)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID", typeof(string));
                    dt.Columns.Add("COUNTRY", typeof(string));
                    dt.Columns.Add("POPULATION", typeof(string));

                    foreach (var item in records)
                    {                       
                        DataRow dr = dt.NewRow();
                        dr["ID"] = (string)item.Element("ID");
                        dr["COUNTRY"] = (string)item.Element("COUNTRY");
                        dr["POPULATION"] = (string)item.Element("POPULATION");
                        dt.Rows.Add(dr);
                    }
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}

In above code I have read the xml file using XDocument. After reading the xml file I have read the xmldocument using linq query. After reading the values by linq query and then added the value into datatable and bind it to the gridviewcontrol.

Now we have done run the application and check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top