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):
<countries>
<country>
<name>ANGOLA</name><code>24</code><size>1345 amp</size>
</country>
<country>
<name>BENIN</name><code>204</code><size>435 amp</size>
</country>
</countries>
- Drag a GridView component from the Toolbox.
- Add to your GridView a PreRender event.
- 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();
}
}