<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ReadXML" %>
Loading a DataSet with XML
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
public partial class ReadXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
panData.Visible = false;
txtXML.Text = "Data.xml";
}
}
protected void btnLoad_Click(object sender, EventArgs e)
{
panData.Visible = true;
string path = "";
labMsg.Text = "";
try
{
DataSet ds1 = new DataSet();
path = txtXML.Text;
if (!path.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
path = Server.MapPath(path);
XmlTextReader xtr = new XmlTextReader(path);
ds1.ReadXml(xtr, XmlReadMode.InferSchema);
foreach (DataTable table in ds1.Tables)
{
Literal caption = new Literal();
caption.Text = "Table: " + table.TableName + "
";
phData.Controls.Add(caption);
GridView grid = new GridView();
grid.CellPadding = 3;
grid.DataSource = table.DefaultView;
grid.DataBind();
phData.Controls.Add(grid);
}
}
catch (IOException)
{
panData.Visible = false;
labMsg.Text = txtXML.Text + " not found";
}
catch
{
panData.Visible = false;
labMsg.Text = path + " unable to be accessed";
}
}
}
File: Data.xml