Introduction
Today, in this article let's play around with one of the interesting and most useful concepts in XML in C#.
Question: What is deleting XML Element Data?
In simple terms "It provides flexibility to delete out the element data from XML and binds the data into grid."
Step 1: Create a new webform project
Step 2: The complete code of Employee.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id>1</Id>
<FirstName>Vijay</FirstName>
<LastName>Prativadi</LastName>
<Age>26</Age>
</Employee>
<Employee>
<Id>2</Id>
<FirstName>Sandeep</FirstName>
<LastName>Reddy</LastName>
<Age>28</Age>
</Employee>
</Employees>
Step 3: The complete code of webform1.aspx looks like this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DeleteXMLNodeApp._Default" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<center>
<div>
<table>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label1" runat="server" Text="Delete XML Element Data" Font-Bold="true"
Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="Please Enter Id" Font-Size="Large" Font-Names="Verdana"
Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Delete XML Data" Font-Names="Verdana"
Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" EnableModelValidation="True" ForeColor="Black"
GridLines="None" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>
<FooterStyle BackColor="Tan"></FooterStyle>
<HeaderStyle BackColor="Tan" Font-Bold="True"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue">
</PagerStyle>
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite"></SelectedRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Emp Id" ReadOnly="true" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>
</td>
</tr>
</table>
</div>
</center>
</form>
</body>
</html>
Step 4: The complete code of webform1.aspx.cs looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
namespace DeleteXMLNodeApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox4.Focus();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(TextBox4.Text))
{
Label5.Text = "Please Enter Some Values"; Label5.ForeColor = System.Drawing.Color.Red;
}
else
{
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("Employee.xml"));
foreach (XmlNode objNode in document.SelectNodes("Employees/Employee"))if (objNode.SelectSingleNode("Id").InnerText == TextBox4.Text)objNode.ParentNode.RemoveChild(objNode);
document.Save(Server.MapPath("Employee.xml"));
GetData(); Label5.Text = "Data Deleted Successfully";
Label5.ForeColor = System.Drawing.Color.Green;
TextBox4.Text = string.Empty;
}
}
protected void GetData()
{
XmlTextReader objTextReader = new XmlTextReader(Server.MapPath("Employee.xml"));
XmlDataDocument objDocument = new XmlDataDocument();
objDocument.DataSet.ReadXml(objTextReader, XmlReadMode.InferSchema);
DataSet objDataSet = objDocument.DataSet;
GridView1.DataSource = objDataSet;
GridView1.DataBind();
}
}
}
Step 5: The output of the application looks like this:
Step 6: The data deleted output of the application looks like this:
Step 7: The complete code of Employee.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Id>1</Id>
<FirstName>Vijay</FirstName>
<LastName>Prativadi</LastName>
<Age>26</Age>
</Employee>
</Employees>
I hope this article is useful for you.