Displaying XML data in DataGrid in ASP.NET

Susanthampy
Posted by Susanthampy under ASP.NET category on | Points: 40 | Views : 2491
1)The .net framework provides the Dataset object which can handle data from variety of sources like SQL, XML etc.

2)The DataSet has a ReadXML() method which is used to read data from an XML file.

Code for binding XML data in DataGrid:

Code:

using System;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strXMLFile = MapPath("products.xml");
lblXMLFileName.Text = strXMLFile;

DataSet objDataSet = new DataSet();
objDataSet.ReadXml(strXMLFile);

dgServers.DataSource = objDataSet.Tables[0].DefaultView;
dgServers.DataBind();

}
}

Xml file products.xml,

Code:

<ProductList>
<Products>
<ProductId>1</ProductId>
<ProductName>Rice</ProductName>
<Rate>27</Rate>
</Products>
<Products>
<ProductId>2</ProductId>
<ProductName>Wheat</ProductName>
<Rate>20</Rate>
</Products>
</ProductList>


This is a simple example .

Comments or Responses

Posted by: T.saravanan on: 4/28/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag its looking good.
Posted by: Susanthampy on: 4/29/2011 Level:Bronze | Status: [Member] [MVP] | Points: 10
nxt time..........definitely
Posted by: Susanthampy on: 5/4/2011 Level:Bronze | Status: [Member] [MVP] | Points: 10
Done

Login to post response