connectseekers.com

Monday, 10 August 2015

Read XML File in Dataset And Bind To GridView In Asp.Net Using C#.Net

8/10/2015 - By Pranav Singh 0

This article will show you how you can read xml file into dataset and bind it to gridview control using c#.net in asp.net.




Here is the ML code.

<?xml version="1.0" encoding="utf-8" ?>
<students>
  <student>
    <id>1</id>
    <name>Student 1</name>
    <address>Address 1</address>
  </student>
  <student>
    <id>2</id>
    <name>Student 2</name>
    <address>Address 2</address>
  </student>
  <student>
    <id>1</id>
    <name>Student 3</name>
    <address>Address 3</address>
  </student>
  <student>
    <id>1</id>
    <name>Student 4</name>
    <address>Address 4</address>
  </student>
</students>

Now we will add gridview control in it.  After adding this control have a look the html code of the page.

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

<!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 in Dataset And Bind To GridView In Asp.Net Using C#.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" Width="100%">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Now take a look of the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication2
{
    public partial class WebForm19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string filepath = @"\XMLDataFile\XMLFile1.xml";
            DataSet objds = new DataSet();
            objds = ReadXMLFile(filepath);
            if (objds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = objds.Tables[0];
                GridView1.DataBind();
            }
        }
        /// <summary>
        /// Function to read xml data in dataset
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private DataSet ReadXMLFile(string fileFinalPath)
        {
            DataSet objds = new DataSet();
            objds.ReadXml(Server.MapPath(fileFinalPath));
            return objds;
        }
    }
}

In above code I have created a function which will read the xml file into the dataset by passing the file path into the dataset ml reader. In above code I have used server.mappath to read the xml file path. Now we have done. Run the application to 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