I have an XML file as follows -
The Autobiography of Benjamin Franklin
8.99
The Confidence Man
11.99
I have to display the content in the above XML in a Table/Gridview (Titles for the Table - Price|Title).
What is the best way to display the above content in a table using ASP.NET(C#)?

Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 7:53 PM EDT
In your aspx
in you code
Regards,
Iftikar
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 28, 2013, 8:25 PM EDT
ex:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Display_xmlfile_gridview_datatable._Default" %>
.CS Code File
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Display_xmlfile_gridview_datatable
{
public partial class _Default : System.Web.UI.Page
{
protected void btn_showdata_Click(object sender, EventArgs e)
{
string Path = Server.MapPath("Data.xml");
DataTable dt = new DataTable("student");
dt.Columns.Add("sid", typeof(System.String));
dt.Columns.Add("sname", typeof(System.String));
dt.Columns.Add("saddress", typeof(System.String));
dt.Columns.Add("smarks", typeof(System.String));
dt.ReadXml(Path);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Data.xml file
r pRank #4,639 · 333 reputationPosted Jul 28, 2013, 11:10 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 11:13 PM EDT
Regards,
Iftikar
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 28, 2013, 11:28 PM EDT
Rosi Sreenivasa Reddy137 reputationPosted Jul 28, 2013, 11:37 PM EDT
this is thank u for this answer.I am also using this answer.
in asp page what header file u use for XmlFile
Sunny SharmaRank #223 · 11.1K reputationPosted Jul 29, 2013, 1:23 AM EDT
I think I have the answer for your question.
As you need the code to remain unchanged even after the schema of the XML file is modified, Yes, there's a way.
Although DataTable doesn't facilitate you to read xml file, create schema and map the data to itself but DataSet does.
Now to answer your question, you can follow the sample code below to get this job done:
-------------------------------------
void BindXmlToGrid()
{
DataSet ds = new DataSet();
ds.ReadXml(@"D:\books.xml");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
----------------------------------------
As you can see, I didn't create any schema and it is mapped to the table at position 0 in dataset. So, this is the way you are seeking for. You don't need to bother about the changes in schema. Yes, do put a try catch block to check if schema is invalid and nothing is imported to DataSet.
Hope it Helps. Happy Coding :)
Cheers!