XML  

Update Data to 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 update data to XML using LINQ to XML?

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

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

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="LINQtoXMLUpdateApp.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="Update 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>  
  29.                         <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName" Font-Size="Large"  
  30.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  31.                     </td>  
  32.                     <td>  
  33.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  34.                     </td>  
  35.                 </tr>  
  36.                 <tr>  
  37.                     <td>  
  38.                         <asp:Label ID="Label3" runat="server" Text="Please Enter LastName" Font-Size="Large"  
  39.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  40.                     </td>  
  41.                     <td>  
  42.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  43.                     </td>  
  44.                 </tr>  
  45.                 <tr>  
  46.                     <td>  
  47.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"  
  48.                             Font-Italic="true"></asp:Label>  
  49.                     </td>  
  50.                     <td>  
  51.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  52.                     </td>  
  53.                 </tr>  
  54.                 <tr>  
  55.                     <td colspan="2" align="center">  
  56.                         <asp:Button ID="Button1" runat="server" Text="Update Data" Font-Names="Verdana" Width="213px"  
  57.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  58.                     </td>  
  59.                 </tr>  
  60.                 <tr>  
  61.                     <td colspan="2" align="center">  
  62.                         <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>  
  63.                     </td>  
  64.                 </tr>  
  65.             </table>  
  66.         </div>  
  67.     </center>  
  68.     </form>  
  69. </body>  
  70. </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 LINQtoXMLUpdateApp  
  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) || string.IsNullOrEmpty(TextBox1.Text)|| string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.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 updateQuery = from r in document.Descendants("Employee")where r.Element("Id").Value == TextBox4.Textselect r;  
  26.                 foreach (var query in updateQuery)  
  27.                 {  
  28.                     query.Element("FirstName").SetValue(TextBox1.Text);  
  29.                     query.Element("LastName").SetValue(TextBox2.Text);  
  30.                     query.Element("Age").SetValue(TextBox3.Text);  
  31.                 }  
  32.                 document.Save(Server.MapPath("Employee.xml"));  
  33.                 Label5.Text = "Data Updated Successfully";  
  34.                 Label5.ForeColor = System.Drawing.Color.Green;  
  35.                 TextBox4.Text = string.Empty;  
  36.                 TextBox1.Text = string.Empty;  
  37.                 TextBox2.Text = string.Empty;  
  38.                 TextBox3.Text = string.Empty;  
  39.             }  
  40.         }  
  41.     }  
  42. }
Step 5: The output of the application looks like this

 

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

Step 6: The data updating to XML output of the application looks like this

data-using-linq-to-xml.png

Step 7: The data updated to 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>Swetha</FirstName>  
  12.     <LastName>Prativadi</LastName>  
  13.     <Age>23</Age>  
  14.   </Employee>  
  15. </Employees>
I hope this article is useful for you.

MVC Corporation is consulting and IT services based company.