how to bind xml file to Gridview
i have Xml file..i want to bind this xml data into gridview..
my file:
<ExtPageList>
<version>E36M3P6</version>
<resultCode>0</resultCode>
<resultStr>Success</resultStr>
<objects>
<ProcessStatus process="monitor">
<id>6062</id>
<condition>running</condition>
<runLevel>7</runLevel>
<state>sleeping</state>
<starts>1</starts>
<uptime>P3DT13H2M37S</uptime>
<fds>21</fds>
</ProcessStatus>
</objects>
<objects>
<ProcessStatus process="manager">
<id>6301</id>
<condition>running</condition>
<runLevel>7</runLevel>
<state>sleeping</state>
<starts>1</starts>
<uptime>P3DT13H2M37S</uptime>
<fds>57</fds>
</ProcessStatus>
</objects>
<totalPages>1</totalPages>
<currentPage>1</currentPage>
<pageSize>19</pageSize>
</ExtPageList>
i want show this data as this formate开发者_C百科 with all data ..
Process Id condition state
easiest way to do is using ReadXml()
of a dataset
DataSet xmlData = new DataSet();
xmlData.ReadXml("D:\\books.xml");
gridControl1.DataSource = xmlData.Tables[0];
Bind xml file and display data of xml file in grid view . here is the code for that
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
DataSet authorsDataSet;
string filePath = Server.MapPath("Authors.xml");
authorsDataSet = new DataSet();
//Read the contents of the XML file into the DataSet
authorsDataSet.ReadXml(filePath);
authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
authorsGird.DataBind();
}
</script>
below is the html markup of grid view
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reading XML Data into a DataSet object </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView id="authorsGird" runat="server"
AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"
HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
<Columns>
<asp:BoundField HeaderText="Last Name" DataField="lastName" />
<asp:BoundField HeaderText="First Name"
DataField="firstName" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Check for the detail : How To Read XML Data into a DataSet by Using Visual C# .NET
string myXMLfile = @"C:\MySchema.xml";
DataSet ds = new DataSet();
// Create new FileStream with which to read the schema.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(myXMLfile, System.IO.FileMode.Open);
try
{
ds.ReadXml(fsReadXml);
dataGrid1.DataSource = ds.Tables[0];
//dataGrid1.DataMember = "Cust";
dataGrid1.DataBind();
}
catch (Exception ex)
{
}
finally
{
fsReadXml.Close();
}
DataSet ds1 = new DataSet();//this dataset to read XML to datagrid
ds1.ReadXml(Server.MapPath(”xmlData.xml”));
gridview.DataSource = ds1.Tables[0].DefaultView;
gridview.DataBind();
All of the answers that have been posted should work;. I think that your issue is that the dataset sees your XML as two tables (not one). Try binding the second table to your gridview, like this:
gridview.DataSource = ds1.Tables[1].DefaultView;
Dataset ds_resp=new Dataset()
ds_resp.ReadXml(Server.MapPath("xmlfiles/PPForms.xml"));
gridview.DataSource = ds_resp;
gridview.DataBind();
精彩评论