XML  

Delete Data From XML Using LINQ to XML

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in LINQ to XML.

Question: What is delete data from XML using LINQ to XML?

In simple terms "It provides flexibility to delete data from XML with the help of a LINQ query."

Step 1: Create a new "ASP.NET Web Application", as in

Output1.jpg

Step 2: The complete code of Employee.xml looks like this

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Employees>  
  3.   <Employee>  
  4.     <Id>1</Id>  
  5.     <FirstName>Vijay</FirstName>  
  6.     <LastName>Prativadi</LastName>  
  7.     <Age>26</Age>  
  8.   </Employee>  
  9.   <Employee>  
  10.     <Id>2</Id>  
  11.     <FirstName>Sandeep</FirstName>  
  12.     <LastName>Reddy</LastName>  
  13.     <Age>28</Age>  
  14.   </Employee>  
  15. </Employees> 
Step 3: The complete code of webform1.aspx looks like this
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LINQtoXMLDeleteApp.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <center>  
  10.         <div>  
  11.             <table>  
  12.                 <tr>  
  13.                     <td colspan="2" align="center">  
  14.                         <asp:Label ID="Label1" runat="server" Text="Delete Data using LINQ-to-XML" Font-Bold="true"  
  15.                             Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td>  
  20.                         <asp:Label ID="Label6" runat="server" Text="Please Enter Id" Font-Size="Large" Font-Names="Verdana"  
  21.                             Font-Italic="true"></asp:Label>  
  22.                     </td>  
  23.                     <td>  
  24.                         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
  25.                     </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td colspan="2" align="center">  
  29.                         <asp:Button ID="Button1" runat="server" Text="Delete Data" Font-Names="Verdana" Width="213px"  
  30.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  31.                     </td>  
  32.                 </tr>  
  33.                 <tr>  
  34.                     <td colspan="2" align="center">  
  35.                         <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>  
  36.                     </td>  
  37.                 </tr>  
  38.             </table>  
  39.         </div>  
  40.     </center>  
  41.     </form>  
  42. </body>  
  43. </html>

Step 4: The complete code of webform1.aspx.cs looks like this

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Xml.Linq;  
  8. namespace LINQtoXMLDeleteApp  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.         }  
  15.         protected void Button1_Click(object sender, EventArgs e)  
  16.         {  
  17.             if (string.IsNullOrEmpty(TextBox4.Text))  
  18.             {  
  19.                 Label5.Text = "Please Enter Some Values";  
  20.                 Label5.ForeColor = System.Drawing.Color.Red;  
  21.             }  
  22.             else  
  23.             {  
  24.                 XDocument document = XDocument.Load(Server.MapPath("Employee.xml"));  
  25.                 var deleteQuery = from r in document.Descendants("Employee")where r.Element("Id").Value == TextBox4.Textselect r;  
  26.                 foreach (var query in deleteQuery){query.Element("Id").Remove();  
  27.                     query.Element("FirstName").Remove();  
  28.                     query.Element("LastName").Remove();  
  29.                     query.Element("Age").Remove();  
  30.                 }  
  31.                 document.Save(Server.MapPath("Employee.xml"));  
  32.                 Label5.Text = "Data Deleted Successfully";  
  33.                 Label5.ForeColor = System.Drawing.Color.Green;  
  34.                 TextBox4.Text = string.Empty;  
  35.             }  
  36.         }  
  37.     }  
  38. }
Step 5: The output of the application looks like this

delete-data-using-linq-to-xml.png

Step 6: The output of the data deleting application looks like this

data-delete-using-linq-to-xml.png

Step 7: The data deleted from XML looks like this

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Employees>  
  3.   <Employee>  
  4.     <Id>1</Id>  
  5.     <FirstName>Vijay</FirstName>  
  6.     <LastName>Prativadi</LastName>  
  7.     <Age>26</Age>  
  8.   </Employee>  
  9.   <Employee />  
  10. </Employees> 
I hope this article is useful for you.

MVC Corporation is consulting and IT services based company.