We can read the contents of an XML file into a DataSet object using the ReadXml() method. This
method reads the rows and columns from the XML file into DataTable objects of the DataSet. For
example, the following statement uses the ReadXml() method to read the XML file myXmlFile.xml
previously written by the WriteXml() method:
myDataSet.ReadXml("myXmlFile.xml");
Below illustrates how to read XML files using ADO.NET.
<DocumentElement>
<employee>
<EMPNO>7369</EMPNO>
<ENAME>SMITH</ENAME>
<JOB>CLERK</JOB>
<MGR>7902</MGR>
<HIREDATE>1980-12-17T00:00:00+05:30</HIREDATE>
<SAL>800</SAL>
<DEPTNO>20</DEPTNO>
</employee>
<employee>
<EMPNO>7499</EMPNO>
<ENAME>ALLEN</ENAME>
<JOB>SALESMAN</JOB>
<MGR>7698</MGR>
<HIREDATE>1981-02-20T00:00:00+05:30</HIREDATE>
<SAL>1600</SAL>
<COMM>300</COMM>
<DEPTNO>30</DEPTNO>
</employee>
</employee>
</DocumentElement>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmDataSetReadXmlFile.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.ADO.FrmDataSetReadXmlFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Read XML File</h1>
<asp:GridView ID="grid" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.IO;
using System.Configuration;
using System.Xml;
namespace LearnAsp.Net.ControlDemo.ADO
{
public partial class FrmDataSetReadXmlFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String FilePath = Server.MapPath("~\\GarbageBin\\myXmlFile.xml");
StringReader theReader = new StringReader(FilePath);
XmlDocument xDoc = new XmlDocument();
xDoc.Load(FilePath);
DataSet ds = new DataSet();
ds.ReadXml(new XmlTextReader(new StringReader(xDoc.DocumentElement.OuterXml)));
grid.DataSource = ds;
grid.DataBind();
}
}
}
No comments:
Post a Comment
Please do not enter any spam link in the comment box.