devxlogo

How to Bind a GridView Control to XML in ASP.NET

How to Bind a GridView Control to XML in ASP.NET

In this example, your XML content is assumed to be ready and well formatted. To be compatible with a GridView, the XML document has to have a database-like format (table and records):

      ANGOLA241345 amp        BENIN204435 amp  
  1. Drag a GridView component from the Toolbox.
  2. Add to your GridView a PreRender event.
  3. Write your code (see below) to bind a DataSet to the GridView.
public partial class _Default : System.Web.UI.Page{	protected void MyGridView_PreRender(object sender, EventArgs e)	{		// Creates a DataSet and loads it with an Xml Content		DataSet aDataSet = new DataSet();		aDataSet.ReadXml(new StringReader(aXmlDoc.OuterXml));		// Bind the DataSet to the grid view		GridView gv = (GridView)sender;		gv.DataSource = aDataSet;		gv.DataBind();	}}
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist