Step By Step How to Insert / Fetch records in/from XML file using ASP.NET 2008.

Hello Everybody,

In this article I have shown you how to insert record in xml file and how to fetch them from xml file using ASP.NET 2008. Here I have used listview controlto display records.

First of all we have to add an xml file in the project. I have taken an xml file named emp.xml.

In this emp.xml file I have added a Root element called

“<Employee> </Employee>”

Inside the above tag now we will add some information about a particular employee like empID,empName and salaryin the inner tag called “<EmpDetails> </EmpDetails>”.

After inserting a record in xml file using asp.net code, the inserted record would look like the below fig.

So let’s start how to insert record in xml file using asp.net 2008.

So here we have our webform named “InsertEmployeeDetails.aspx”

In the code behind part of this webform we have to add a namespace called

Imports System.Xml

And in the “save” button_click event we have to write the below code.

ProtectedSub btnsave_Click(ByVal sender AsObject, ByVal e As EventArgs) Handles btnsave.Click

Try

Dim emp_xml_doc AsNew XmlDocument

If System.IO.File.Exists(Server.MapPath("emp.xml")) Then

emp_xml_doc.Load(Server.MapPath("emp.xml"))

Dim myrow_element As XmlElement

myrow_element = emp_xml_doc.CreateElement("EmpDetails")

Dim str AsString

str = "<EmpID>" & TxtEmpId.Text & "</EmpID>" & _

"<Empname>" & TxtName.Text & "</Empname>" & _

"<EmpSalary>" & TxtSalary.Text & "</EmpSalary>"

myrow_element.InnerXml = str

emp_xml_doc.DocumentElement.AppendChild(myrow_element)

emp_xml_doc.Save(Server.MapPath("emp.xml"))

Response.Write("Record Saved")

Else

Response.Write("File does not exist.")

EndIf

Catch ex As Exception

Response.Write(ex.ToString)

EndTry

EndSub

Now we run this page and we get.

(click on “Save”button)

If we see the “emp.xml” file we get the inserted record.

Now let’s see how to fetch record from xml file and view in the webform.

For displaying records I have taken listview control.

br/>

h2 View All Employee Details.</h2

div

asp:ListViewid="lsvEmp"runat="server">

LayoutTemplate

spanid="ItemPlaceHolder"runat="server"> <!-- you must have to add this line -->

</span

</LayoutTemplate

ItemTemplate

tableborder="0px"width="450px"cellpadding="0px"cellspacing="0px"align="center">

tr

tdwidth="100px"style="color:Green ; font-family:Arial ; font-size :14px"

asp:Labelid="lblEmpid"runat="server"Text='<%# XPath("EmpID") %>'>

</asp:Label

</td

tdwidth="250px"style="color:Blue ; font-family:Arial ; font-size :14px"

asp:Labelid="lblempname"runat="server"Text='<%# XPath("Empname") %>'>

</asp:Label

</td

tdwidth="100px"style="color:Red ; font-family:Arial ; font-size :14px"

asp:Labelid="lblsalary"runat="server"Text='<%# XPath("EmpSalary") %>'>

</asp:Label

</td

</tr

</table

</ItemTemplate

</asp:ListView

</div

Due to the above code, the design view of the webform looks like below ;

Now let’s connect the above listview control with the “emp.xml” file.

Step 1 :

Step 2 :

(Select <New data source….>.)

Step 3 :

(Select xml file and click “ok”)

Step 4 :

(Browse Data file )

Step 5:

(Select “emp.xml” )

Step 6 :

(click on “Ok”)

Now run this Webform

if you want to insert more record in the emp.xml file and with the help of view button you want to see the records. Then in view button_click event code behind you have to write the below code.

ProtectedSub BtnView_Click(ByVal sender AsObject, ByVal e As EventArgs) Handles BtnView.Click

lsvEmp.DataSourceID = "XmlDataSource1"

EndSub

Now run this webform again.

(Click “Save” button to save record in emp.xml file)

(Now click on “View” button to view record on the listview.)

So thank you very much.

Enjoy programming.

Developed by :

Amarjit singh

(Software developer.)

(If you have any doubt or suggestions so please email me at: .)