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 insert data to XML using LINQ to XML?

In simple terms "It provides flexibility to insert data into 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="LINQtoXMLInsertApp.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="Insert 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="Insert 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 LINQtoXMLInsertApp
  9. {
  10. public partial class WebForm1 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. TextBox4.Focus();
  15. }
  16. protected void Button1_Click(object sender, EventArgs e)
  17. {
  18. if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text))
  19. {
  20. Label5.Text = "Please Enter Some Values";
  21. Label5.ForeColor = System.Drawing.Color.Red;
  22. }
  23. else
  24. {
  25. XDocument document = XDocument.Load(Server.MapPath("Employee.xml"));
  26. document.Element("Employees").Add(new XElement("Employee", new XElement("Id", TextBox4.Text), new XElement("FirstName", TextBox1.Text), new XElement("LastName", TextBox2.Text), new XElement("Age", TextBox3.Text)));
  27. document.Save(Server.MapPath("Employee.xml"));
  28. Label5.Text = "Data Inserted Successfully";
  29. Label5.ForeColor = System.Drawing.Color.Green;
  30. TextBox4.Text = string.Empty;
  31. TextBox1.Text = string.Empty;
  32. TextBox2.Text = string.Empty;
  33. TextBox3.Text = string.Empty;
  34. }
  35. }
  36. }
  37. }
Step 5: The output of the application looks like this

Insert-Data-using-linq-to-xml.png

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

Data-using-linq-to-xml.png

Step 7: The data inserted 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>3</Id>
  17. <FirstName>Ajit</FirstName>
  18. <LastName>Kumar</LastName>
  19. <Age>26</Age>
  20. </Employee>
  21. </Employees>
I hope this article is useful for you.