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:
- <?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 - How to create XML Document.
- How to Perform CRUD Operation in XML Document.
Step 1: Firstly, I will create a XML document and save the document any location.
- <?xml version="1.0" encoding="utf-8"?>
- <Employess>
- <Employee>
- <id>101</id>
- <name>Surya</name>
- <salary>100000</salary>
- <email>[email protected]</email>
- <address>noida</address>
- </Employee>
- <Employee>
- <id>102</id>
- <name>Amit</name>
- <salary>12000</salary>
- <email>[email protected]</email>
- <address>Delhi</address>
- </Employee>
- <Employee>
- <id>120</id>
- <name>Surya</name>
- <salary>1500</salary>
- <email>Surya</email>
- <address>12222</address>
- </Employee>
- <Employee>
- <id>105</id>
- <name>Surya</name>
- <salary>150000</salary>
- <email>Surya</email>
- <address>12222</address>
- </Employee>
- <Employee>
- <id>105</id>
- <name>Amar</name>
- <salary>4522</salary>
- <email>[email protected]</email>
- <address>Noida</address>
- </Employee>
- <Employee>
- <id>103</id>
- <name>Surya</name>
- <salary>12000</salary>
- <email>[email protected]</email>
- <address>new delhi</address>
- </Employee>
- </Employess>
Step 2: - Open the Visual studio and create an empty web application.
- Add a webform.
- Add some web controls like Gridview(Using show XML data), Textbox, Button.
- Add Namespace using System.XML.LINQ;
Like:
Step 3: Bind the XML data in GridView.
Firstly, add namesapace System.XML.LINQ;
Source Code:
-
- XDocument xmldoc;
- public void BindGrid()
- {
- xmldoc = XDocument.Load("D:/Emp12.xml");
- var bind = xmldoc.Descendants("Employee").Select(p => new
- {
- Id=p.Element("id").Value,
- Name=p.Element("name").Value,
- Salary=p.Element("salary").Value,
- Email=p.Element("email").Value,
- Address=p.Element("address").Value
- }).OrderBy(p=>p.Id);
- GridView1.DataSource = bind;
- GridView1.DataBind();
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- BindGrid();
- }
Step 4: Perform CRUD operation.
- Code For Insert Data in XML document
- protected void Insert_Click(object sender, EventArgs e)
- {
- XElement emp = new XElement("Employee",
- new XElement("id", txtid.Text),
- new XElement("name", txtname.Text),
- new XElement("salary",txtsalary.Text),
- new XElement("email",txtemail.Text),
- new XElement("address", txtaddress.Text));
- xmldoc.Root.Add(emp);
- xmldoc.Save("D:/Emp12.xml");
- BindGrid();
- Reset();
-
- }
for reset():
- private void Reset()
- {
- txtid.Text = "";
- txtname.Text = "";
- txtsalary.Text = "";
- txtaddress.Text = "";
- txtemail.Text = "";
- txtid.Focus();
- }
- Code For Find Data by Id in XML document
- protected void Find(object sender, EventArgs e)
- {
- XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);
- if (emp != null)
- {
- txtname.Text = emp.Element("name").Value;
- txtsalary.Text = emp.Element("salary").Value;
- txtemail.Text = emp.Element("email").Value;
- txtaddress.Text = emp.Element("address").Value;
-
- }
- }
- Code For Update by Id in XML document
- protected void Update_click(object sender, EventArgs e)
- {
-
- XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);
- if (emp != null)
- {
- emp.Element("name").Value = txtname.Text;
- emp.Element("salary").Value = txtsalary.Text;
- emp.Element("email").Value = txtemail.Text;
- emp.Element("address").Value = txtaddress.Text;
- xmldoc.Root.Add(emp);
- xmldoc.Save("D:/Emp12.xml");
- BindGrid();
- Reset();
-
- }
- }
- Code For Delete by Id in XML document
- protected void delete_click(object sender, EventArgs e)
- {
- XElement emp = xmldoc.Descendants("Employee").FirstOrDefault(p => p.Element("id").Value == txtid.Text);
- if (emp != null)
- {
- emp.Remove();
- xmldoc.Save("D:/Emp12.xml");
- BindGrid();
- Reset();
- }
- }
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.