Read XML into Datset,Gridview

In Asp.Net we can read the XML file using Dataset object and we can displays it using Gridview or any other Datacontrol.

Normally the XML file look like this


<?xml version="1.0" encoding="utf-8" ?> <Employees> <employee> <id>1</id> <name>A</name> <salary>10000</salary> </employee> <employee> <id>2</id> <name>B</name> <salary>20000</salary> </employee> . . . . . . . . . . <Employees>

The first line of the code displays the Xml file version and encoding format for XML file
The second line displays the start of the root element and after that all are child elements.
Here <Employees> is the root element. It contains the several child elements like <employee> (employee information).

We need to read this data into dataset and display using Gridview.
For that we need to import namespace called System.Data


Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ReadXML() End Sub Sub ReadXML() Try Dim ds As New DataSet ds.ReadXml(Server.MapPath("Employee.xml")) gv_XML.DataSource = ds.Tables(0) gv_XML.DataBind() Catch ex As Exception End Try End Sub End Class

We can read the XML file by using Dataset ReadXml method. It requries XML file path .

Ds.ReadXml(server.MapPath(“XML File path”))

Dataset reads the XML file as a single table.

After that we will bind the dataset table to Gridview control.


Download source code here