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.
Some of my previous articles are as follows: Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net,
Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Bind
Asp.net DropdownList Control by XML File Data Using DataSet in C#.Net, Bind
Asp.net ListBox Control by XML File Data Using DataSet in C#.Net, Image
Save In XML and Load XML Bitmap Image File in Windows Application Using C#.Net.
<?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.
0 comments:
Please let me know your view