CRUD Operation Using LINQ To XML Documents

This article is easy to learn LINQ to XML Document.

Introduction

XML stands for EXtensible Markup Language. It's use to store and transport data. It is a textual data format with strong support via Unicode for different human languages.

It's developed by the W3C.

XML declaration

Defining the head of XML file:

  1. <?XML version="1.0" encoding="UTF-8"?>  
Relationship between XML elements and LINQ to XML classes: 

    XML Document -- XDocument,
    XML Element --XElement,
    XML Element Name --XName,
    XML Element Attribute --XAttribute,
    XML Node --XNode

In this article I'm going to share about
  1. How to create XML Document.
  2. How to Perform CRUD Operation in XML Document.

Step 1: Firstly, I will create a XML document and save the document any location.

  1.    <?xml version="1.0" encoding="utf-8"?>  
  2. <Employess>  
  3.   <Employee>  
  4.     <id>101</id>  
  5.     <name>Surya</name>  
  6.     <salary>100000</salary>  
  7.     <email>[email protected]</email>  
  8.     <address>noida</address>  
  9.   </Employee>  
  10.   <Employee>  
  11.     <id>102</id>  
  12.     <name>Amit</name>  
  13.     <salary>12000</salary>  
  14.     <email>[email protected]</email>  
  15.     <address>Delhi</address>  
  16.   </Employee>  
  17.   <Employee>  
  18.     <id>120</id>  
  19.     <name>Surya</name>  
  20.     <salary>1500</salary>  
  21.     <email>Surya</email>  
  22.     <address>12222</address>  
  23.   </Employee>  
  24.   <Employee>  
  25.     <id>105</id>  
  26.     <name>Surya</name>  
  27.     <salary>150000</salary>  
  28.     <email>Surya</email>  
  29.     <address>12222</address>  
  30.   </Employee>  
  31.   <Employee>  
  32.     <id>105</id>  
  33.     <name>Amar</name>  
  34.     <salary>4522</salary>  
  35.     <email>[email protected]</email>  
  36.     <address>Noida</address>  
  37.   </Employee>  
  38.   <Employee>  
  39.     <id>103</id>  
  40.     <name>Surya</name>  
  41.     <salary>12000</salary>  
  42.     <email>[email protected]</email>  
  43.     <address>new delhi</address>  
  44.   </Employee>  
  45. </Employess>  
Step 2:
  1. Open the Visual studio and create an empty web application.
  2. Add a webform.
  3. Add some web controls like Gridview(Using show XML data), Textbox, Button.
  4. Add Namespace using System.XML.LINQ;

Like:

table

Step 3: Bind the XML data in GridView.

Firstly, add namesapace System.XML.LINQ;

Source Code:

  1. //Bind the Grid  
  2. XDocument xmldoc;        
  3. public void BindGrid()  
  4. {  
  5.     xmldoc = XDocument.Load("D:/Emp12.xml");   //add xml document  
  6.     var bind = xmldoc.Descendants("Employee").Select(p => new  
  7.     {  
  8.         Id=p.Element("id").Value,  
  9.         Name=p.Element("name").Value,  
  10.         Salary=p.Element("salary").Value,  
  11.         Email=p.Element("email").Value,  
  12.          Address=p.Element("address").Value  
  13.     }).OrderBy(p=>p.Id);  
  14.     GridView1.DataSource = bind;  
  15.     GridView1.DataBind();  
  16. }  
  17. protected void Page_Load(object sender, EventArgs e)  
  18. {  
  19.     BindGrid();  
  20. }  
Step 4: Perform CRUD operation.
  1. Code For Insert Data in XML document
    1. protected void Insert_Click(object sender, EventArgs e)  
    2. {  
    3.     XElement emp = new XElement("Employee",  
    4.         new XElement("id", txtid.Text),  
    5.         new XElement("name", txtname.Text),  
    6.         new XElement("salary",txtsalary.Text),  
    7.         new XElement("email",txtemail.Text),  
    8.         new XElement("address", txtaddress.Text));  
    9.     xmldoc.Root.Add(emp);  
    10.     xmldoc.Save("D:/Emp12.xml");  
    11.     BindGrid();  
    12.     Reset(); // For clear textbox  
    13.   
    14. }  
    for reset():
    1. private void Reset()  
    2.   {  
    3.       txtid.Text = "";  
    4.       txtname.Text = "";  
    5.       txtsalary.Text = "";  
    6.       txtaddress.Text = "";  
    7.       txtemail.Text = "";  
    8.       txtid.Focus();  
    9.   }  
  2. Code For Find Data by Id in XML document
    1. protected void Find(object sender, EventArgs e)  
    2. {  
    3.     XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);  
    4.     if (emp != null)  
    5.     {  
    6.         txtname.Text = emp.Element("name").Value;  
    7.         txtsalary.Text = emp.Element("salary").Value;  
    8.         txtemail.Text = emp.Element("email").Value;  
    9.         txtaddress.Text = emp.Element("address").Value;  
    10.          
    11.     }  
    12. }  
  3. Code For Update by Id in XML document
    1. protected void Update_click(object sender, EventArgs e)  
    2. {  
    3.   
    4.     XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);  
    5.     if (emp != null)  
    6.     {  
    7.         emp.Element("name").Value = txtname.Text;  
    8.         emp.Element("salary").Value = txtsalary.Text;  
    9.         emp.Element("email").Value = txtemail.Text;  
    10.         emp.Element("address").Value = txtaddress.Text;  
    11.         xmldoc.Root.Add(emp);  
    12.         xmldoc.Save("D:/Emp12.xml");  
    13.         BindGrid();  
    14.         Reset();  
    15.   
    16.     }  
    17. }  
  4. Code For Delete by Id in XML document
    1. protected void delete_click(object sender, EventArgs e)  
    2. {  
    3.     XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);  
    4.     if (emp != null)  
    5.     {  
    6.         emp.Remove();  
    7.         xmldoc.Save("D:/Emp12.xml");  
    8.         BindGrid();  
    9.         Reset();  
    10.     }  
    11. }  

Important Question:

Difference between XElement and XDocument?

Answer: xDcument represent a whole XML Document.

xElement represent an XML element. It's part of longer document.

Hope this article helps you. Thanks for reading.


Similar Articles