Description:-
Here
in this example I will Explain that How to Insert data into an XML File and
read data From the XML File and Bind to the DataList control in asp.Net.
In Previous we also explain that How
to insert data into an XML file and Bind to Gridview but here we use DataList
Control.
First of all we understand what is
XML:-
XML means
Extensible markup language why we need to use this one because by using XML we
can store the data and we can easily retrieve or read data from XML file and
display the data without using database in our applications.
if we need to display dynamic data in our application it
will take long time to conenct database and Fetch data from the database but if we are use XML to store data we can easily perform
operations with xml file directly without using any connection or database. If
we store data in database that is incompatible and need connection to some of
the computer applications but if we store data in XML format it will support
for all applications.
Now I will explain inserting data into xml and retrive data
from XML and bind data to datalist with simple example.
to show Example of How to Bind XML data to Gridview click here Insert/Read from XML file and bind to Gridview
to Download the Complete Project plz click below Download Image Link
to Download the Complete Project plz click below Download Image Link
BindxmldatatoDataList.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BindxmldatatoDataList.aspx.cs" Inherits="BindxmldatatoDataList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Location:</td>
<td style="width: 100px">
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Email:</td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px" valign="top">
Comments:</td>
<td style="width: 100px">
<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="104px"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /></td>
</tr>
</table>
<br />
<asp:DataList ID="dlComments" Runat="server" Width="100%">
<ItemTemplate>
<hr size=0/>
Name: <%# DataBinder.Eval(Container.DataItem,
"name") %><br />
E-mail: <a href="mailto:<%# DataBinder.Eval(Container.DataItem,
"email") %>"><%# DataBinder.Eval(Container.DataItem,
"email") %></a><br />
Location: <%# DataBinder.Eval(Container.DataItem,
"location") %><br />
Date: <%# DataBinder.Eval(Container.DataItem,
"Date") %><br />
Description: <%# DataBinder.Eval(Container.DataItem,
"Description") %>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Demo.xml:-
<?xml version="1.0" encoding="utf-8"?>
<CommentsInformation>
</CommentsInformation>
BindxmldatatoDataList.aspx.cs:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Xml;
public partial class BindxmldatatoDataList :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if
(!IsPostBack)
{
//Bind xml data to datalist
BindDatalist();
}
}
protected void
btnSubmit_Click(object sender, EventArgs e)
{
XmlDocument
xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("Demo.xml"));
XmlElement
parentelement = xmldoc.CreateElement("Comments");
XmlElement
name = xmldoc.CreateElement("Name");
name.InnerText =
txtName.Text;
XmlElement
location = xmldoc.CreateElement("location");
location.InnerText =
txtLocation.Text;
XmlElement
email = xmldoc.CreateElement("Email");
email.InnerText =
txtEmail.Text;
XmlElement
Description = xmldoc.CreateElement("Description");
Description.InnerText =
txtComments.Text;
XmlElement
date = xmldoc.CreateElement("Date");
date.InnerText = DateTime.Now.ToString();
parentelement.AppendChild(name);
parentelement.AppendChild(location);
parentelement.AppendChild(email);
parentelement.AppendChild(Description);
parentelement.AppendChild(date);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("Demo.xml"));
BindDatalist();
}
/// <summary>
///
Bind xml data to datalist
/// </summary>
private void
BindDatalist()
{
XmlTextReader
xmlreader = new XmlTextReader(Server.MapPath("Demo.xml"));
DataSet ds
= new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if
(ds.Tables.Count != 0)
{
dlComments.DataSource = ds;
dlComments.DataBind();
}
else
{
dlComments.DataSource = null;
dlComments.DataBind();
}
}
}
0 comments:
Post a Comment