In this article we will learn how to read an XML file directly into dataset by using readxml() method of the Dataset class.
Suppose we have an XML file like this
<Employee> <item> <name> Hirendra </name> <Age> 25 </Age> </item> <item> <name> Ankur </name> <Age> 28 </Age> </item> </Employee>
Now we have to create a dataset and show in the datagrid control, like see below:
You can see below that in the page_Load, an instance of the dataset is created with name dsmain. We use the ReadXml() method is called to read employee.xml file. and in the last this dataset is bound to the datagrid control.
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default3.aspx.vb" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace="System.Data" %> <script runat="server"> Sub Page_Load() Dim dsmain As New DataSet dsmain.ReadXml(MapPath("XML file\Employee.xml")) DataGrid1.DataSource = dsmain DataGrid1.DataBind() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <div><asp:DataGrid ID="DataGrid1" runat="server"> </asp:DataGrid></div> </body> </html>