connectseekers.com

Thursday, 6 August 2015

Read XML File in DataTable and Bind to DataList In Asp.Net Using C#.Net

8/06/2015 - By Pranav Singh 0

This article will show you how you can read XML file data into datatable and bind the data into datalist control and display vertically in asp.net using c#.net.


So for this article first we will create a new asp.net application and add an XML file for data.

<?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 a button control and a datalist control on page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.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 in DataTable and Bind to DataList  In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Click To Read XML" OnClick="Button1_Click" />
    </div>
    <asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#3366CC"
        BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Both">
        <ItemStyle BackColor="White" ForeColor="#003399" />
        <ItemTemplate>
            Id :
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label><br />
            Name :
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label><br />
            Address :
            <asp:Label ID="Label3" runat="server" Text='<%# Eval("address") %>'></asp:Label>
        </ItemTemplate>
    </asp:DataList>
    </form>
</body>
</html>

I have already bind the datalist control with data. Now add the below code into the page.

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 WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataTable objDataTable = new DataTable();
            string filepathe = @"XMLFile\XMLFile1.xml";
            objDataTable = ReadXMLFile(filepathe);
            DataList1.DataSource = objDataTable;
            DataList1.DataBind();
        }
        /// <summary>
        /// Function to read xml data in datatable
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private DataTable ReadXMLFile(string filePath)
        {
            DataSet objds = new DataSet();
            objds.ReadXml(Server.MapPath(filePath));
            return objds.Tables[0];
        }
    }
}

In above code I have read the code and xml file first and t then bind It to datalist. Now we have done run the code to 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