Add Attribute to XML Using LINQ to XML

Today, in this article let’s play around with one of the interesting and most useful concepts in LINQ to XML. add a new XAttribute to XML with help of a LINQ query.

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 adding attribute to XML using LINQ to XML?

In simple terms "It provides flexibility to add a new XAttribute to XML with help of a LINQ query."

Step 1: Create an "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="LINQtoXMLAddAttributeApp.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="Create Attribute 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.                     <td>  
  27.                         <asp:Label ID="Label7" runat="server" Text="Please Enter Attribute Value" Font-Size="Large"  
  28.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  29.                     </td>  
  30.                     <td>  
  31.                         <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>  
  32.                     </td>  
  33.                 </tr>  
  34.                 <tr>  
  35.                     <td>  
  36.                         <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName" Font-Size="Large"  
  37.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  38.                     </td>  
  39.                     <td>  
  40.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  41.                     </td>  
  42.                 </tr>  
  43.                 <tr>  
  44.                     <td>  
  45.                         <asp:Label ID="Label3" runat="server" Text="Please Enter LastName" Font-Size="Large"  
  46.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  47.                     </td>  
  48.                     <td>  
  49.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  50.                     </td>  
  51.                 </tr>  
  52.                 <tr>  
  53.                     <td>  
  54.                         <asp:Label ID="Label4" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"  
  55.                             Font-Italic="true"></asp:Label>  
  56.                     </td>  
  57.                     <td>  
  58.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  59.                     </td>  
  60.                 </tr>  
  61.                 <tr>  
  62.                     <td colspan="2" align="center">  
  63.                         <asp:Button ID="Button1" runat="server" Text="Create Data" Font-Names="Verdana" Width="213px"  
  64.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  65.                     </td>  
  66.                 </tr>  
  67.                 <tr>  
  68.                     <td colspan="2" align="center">  
  69.                         <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>  
  70.                     </td>  
  71.                 </tr>  
  72.             </table>  
  73.         </div>  
  74.     </center>  
  75.     </form>  
  76. </body>  
  77. </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 LINQtoXMLAddAttributeApp  
  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) || string.IsNullOrEmpty(TextBox5.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.                 document.Element("Employees").Add(new XElement("Employee"new XElement("Id"new XAttribute("Code", TextBox5.Text), TextBox4.Text), new XElement("FirstName", TextBox1.Text), new XElement("LastName", TextBox2.Text), new XElement("Age", TextBox3.Text)));  
  26.                 document.Save(Server.MapPath("Employee.xml"));  
  27.                 Label5.Text = "Data Created Successfully";  
  28.                 Label5.ForeColor = System.Drawing.Color.Green; TextBox4.Text = string.Empty;  
  29.                 TextBox1.Text = string.Empty;  
  30.                 TextBox2.Text = string.Empty;  
  31.                 TextBox3.Text = string.Empty;  
  32.                 TextBox5.Text = string.Empty;  
  33.             }  
  34.         }  
  35.     }  
  36. }
Step 5: The output of the application looks like this:

create-attribute-using-linq-to-xml.jpg

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

attribute-using-linq-to-xml.jpg

Step 7: The attribute added 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>Sandeep</FirstName>  
  12.     <LastName>Reddy</LastName>  
  13.     <Age>28</Age>  
  14.   </Employee>  
  15.   <Employee>  
  16.     <Id Code="301">3</Id>  
  17.     <FirstName>Raj</FirstName>  
  18.     <LastName>Kumar</LastName>  
  19.     <Age>22</Age>  
  20.   </Employee>  
  21. </Employees>
I hope this article is useful for you.