. Linq To XML (Add ,edit ,delete ): -
One of the intresting topic is xml. As a programmer I am not using XML in my daily use but I like XML.
Today I just wondering to take a suitable example which will do add,edit & delete with LINQ. So first we have to Create a xml file for that lets we created a xml file with following format.
<?xml version=“1.0“ encoding=“utf-8“?>
<Trainings>
<Training>
<Id>2</Id>
<Person>RAJAT JAISWAL</Person>
<FrameWork>2</FrameWork>
<ASPNET>2</ASPNET>
<Communication>4</Communication>
<HTML>5</HTML>
<LogicBuilding>4</LogicBuilding>
<SqlServer>5</SqlServer>
<Remark>testing</Remark>
</Training>
</Trainings>
Now if we want to add new record in this xml file then our first moto is to read the xml using linq . For this we first use a import statement
Imports System.Xml.Linq by this we are able to use Linq over xml.
Now for reading we just do same as we do for database read (linq to sql ) but before that we have to create the object of xdocument from where the data will be fatch. As shown below
Dim TrainXDoc As XDocument = XDocument.Load(Server.MapPath(“MyTraining.xml”))
What this statement does it create same object like dataContext in sql.
And below is linq over this document and the Descendants(“Training”) like table or other object like storeprocedure in dbContext.
Dim lnqTraining = From myTraining In TrainXDoc.Descendants(“Training”) _
Select New With {.Id = myTraining.Element(“Id”).Value, _
.Person = myTraining.Element(“Person”).Value, _
.FrameWork = myTraining.Element(“FrameWork”).Value, _
.ASPNET = myTraining.Element(“ASPNET”).Value, _
.SqlServer = myTraining.Element(“SqlServer”).Value, _
.LogicBuilding = myTraining.Element(“LogicBuilding”).Value, _
.HTML = myTraining.Element(“HTML”).Value, _
.Communication = myTraining.Element(“Communication”).Value, _
.Remark = myTraining.Element(“Remark”).Value}
In the above elements like a row in datatable and we access there column using the tag name like Id, Person,Framework.
Now our second moto is to Add & Update the Add a new record can be done as follows
TrainXDoc .Element(“Trainings”).Add(New XElement(“Training”, New XElement(“Id”, lngId), New XElement(“Person”, Me.txtName.Text.Trim), _
New XElement(“FrameWork”, ddlFramWork.SelectedValue), New XElement(“ASPNET”, ddlAspNet.SelectedValue), _
New XElement(“Communication”, ddlCommunication.SelectedValue), New XElement(“HTML”, Me.ddlHtml.SelectedValue), _
New XElement(“LogicBuilding”, Me.ddlLogicBuilding.SelectedValue), New XElement(“SqlServer”, Me.ddlSqlServer.SelectedValue), _
New XElement(“Remark”, Me.txtRemark.Text.Trim)))
Here it added new record in traindoc’s trainings element which was root element.
After this we use MyTrainXDoc.Save(Server.MapPath(“MyTraining.xml”)) which save in the file.
For update we just find the node using the linq which we want to update then made changes in the particular node elements. And call save method as above.
For delete just find the element from linq then remove like xRoot.Remove() then save the xdoc.
For more detail just download the code from
http://www.indiandotnet.tk/
Thanks & Regards
Rajat Jaiswal