Xml:
Extensible Markup Language (XML) is used to describe data. The XML standard is a
flexible way to create information formats and electronically share structured
data via the public Internet, as well as via corporate networks. XML is actually a simpler and easier-to-use
subset of the Standard Generalized Markup
Language (SGML), which is the standard to create a document
structure.
What is XML?
- XML stands for Extensible Markup Language
- XML is a markup language much like
HTML.
- XML was designed to describe data.
- XML tags are not predefined
in XML. You must define your
own tags.
- XML is self describing.
- XML uses a DTD (Document Type Definition) to
formally describe the data.
Difference between XML and HTML
1. HTML is about displaying information, XML is about describing information.
2. XML is not a replacement for HTML.
3. XML and HTML were designed with different goals:
4. XML was designed to describe data and to focus on what data is.
5. HTML was designed to display data and to focus on how data looks.
How to save, delete,
update, select data from Xml in Asp.Net C#
First we
will create website and add Employees.xml file
Sources Code for Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td colspan="2"><b><i><u>How to
save,delete,update ,select data
from Xml in Asp.Net C#</u></i></b></td>
</tr>
<tr>
<td>EmpId</td>
<td>
<asp:TextBox ID="txtempid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:RadioButtonList ID="rblGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td>EmailId</td>
<td>
<asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Department</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>Account</asp:ListItem>
<asp:ListItem>CS IT</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnsave" runat="server" OnClick="btnsave_Click" Text="Save" />
<asp:Button ID="btnsearch" runat="server" OnClick="btnsearch_Click" Text="Search" />
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />
<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />
<asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Text="Check" />
<asp:Button ID="btnAllData" runat="server" OnClick="btnAllData_Click" Text="ShowAllData" />
<asp:Button ID="btnRemoveAll" runat="server" OnClick="btnRemoveAll_Click" Text="Remove
All"
/>
</td>
</tr>
<tr>
<td colspan="2">
<center><asp:Label ID="lblmessage" runat="server" Text="Label" Visible="false"></asp:Label></center>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView></center>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Sources Code for Default.aspx.cs
using System;
using
System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using System.Xml;
// using this name sapce
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
//
create class Employee................
class Employee
{
public int EmpId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public string Department { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
int empid;
XDocument document = XDocument.Load(@"C:\Users\user\Desktop\XmlWithAsp\Data\Employees.xml");
//
code for How to save data in Employee.xml
file..................
protected void btnsave_Click(object sender, EventArgs e)
{
try
{
document.Element("Employees").Add(
new XElement("Employee",
new XElement("EmpId", Convert.ToInt32(txtempid.Text)),
new XElement("Name", txtname.Text),
new XElement("Gender",
rblGender.SelectedValue),
new XElement("Email", txtemailid.Text),
new XElement("Department",
ddlDepartment.SelectedItem.Text)
));
document.Save(Server.MapPath("~/Data/Employees.xml"));
//document.Save(@"C:\Users\user\Desktop\XmlWithAsp\Data\Employees.xml");
document.Save(@"C:\Users\user\Desktop\XmlWithAsp\Data\Employees.xml");
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Save
Successfully";
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = ex.Message;
}
}
//
code for How to search data from Employee.xml file with
EmpId..................
protected void btnsearch_Click(object sender, EventArgs e)
{
empid = Convert.ToInt32(txtempid.Text);
var query = from r in
document.Descendants("Employee")
where (int)r.Element("EmpId") == empid
select new
{
Name = r.Element("Name").Value,
Gender = r.Element("Gender").Value,
Email = r.Element("Email").Value,
Department = r.Element("Department").Value
};
foreach (var item in query)
{
txtname.Text = item.Name;
txtemailid.Text = item.Email;
rblGender.SelectedValue =
item.Gender;
ddlDepartment.SelectedValue =
item.Department;
}
//GridView1.DataSource =
query;
//GridView1.DataBind();
}
protected void btnCheck_Click(object sender, EventArgs e)
{
string email = (from r in
document.Descendants("Employee")
where (string)r.Element("Name") == txtname.Text
select r.Element("Email").Value).Single();
// output---- EmailId for example Kush Tiwari=>
kushktiwari@gmail.com
// string email =
document.Descendants("Employee").Where(i =>
i.Element("Name").Value == "Kush").Select(i =>
i.Element("Email").Value).Single();
Response.Write(email);
bool IsExist =
document.Descendants("Employee").Where(i => i.Element("Name").Value ==
txtname.Text).Select(i => i).Any();
Response.Write(IsExist.ToString());
// output---- Name will be
exit in xml file it will return true other wise false...........
}
// code for How to
show all data from Employee.xml file..................
protected void btnAllData_Click(object sender, EventArgs e)
{
//DataSet ds = new DataSet();
//ds.ReadXml(Server.MapPath("~/Data/Employees.xml"));
//GridView1.DataSource = ds;
//GridView1.DataBind();
var query = from r in
document.Descendants("Employee")
select new
{
EmpId = r.Element("EmpId").Value,
Name = r.Element("Name").Value,
Gender = r.Element("Gender").Value,
Email = r.Element("Email").Value,
Department = r.Element("Department").Value
};
GridView1.DataSource =
query.ToList();
GridView1.DataBind();
}
//
code for How to delete data in
Employee.xml file with
EmpId.............
protected void btnDelete_Click(object sender, EventArgs e)
{
document.Element("Employees").Elements("Employee").Where(i =>
i.Element("EmpId").Value
== txtempid.Text).Remove();
document.Save(Server.MapPath("~/Data/Employees.xml"));
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Deleted
Successfully";
}
//
code for How to update data in
Employee.xml file with EmpId..................
protected void btnUpdate_Click(object sender, EventArgs e)
{
XElement element =
document.Descendants("Employee").Where(m => m.Element("EmpId").Value ==
txtempid.Text.Trim()).Single();
element.SetElementValue("Name", txtname.Text);
element.SetElementValue("Gender",
rblGender.SelectedValue);
element.SetElementValue("Email",
txtemailid.Text.Trim());
element.SetElementValue("Department", ddlDepartment.SelectedItem.Text);
document.Save(@"C:\Users\user\Desktop\XmlWithAsp\Data\Employees.xml");
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Data updated
Successfully";
}
//
code for How to remove all data from Employee.xml file..................
protected void btnRemoveAll_Click(object sender, EventArgs e)
{
document.Element("Employees").Elements("Employee").Remove();
document.Save(Server.MapPath("~/Data/Employees.xml"));
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Remove All
Data Successfully";
}
}
Thank you for sharing your thoughts and knowledge on this topic. This is really helpful and informative, as this gave me more insight to create more ideas and solutions for my plan. I would love to see more updates from you.
ReplyDeleteWeb Development Services