如何将xml文件转换为gridview [英] how to convert xml file to gridview
问题描述
我有一个样本XML文件.由此,我想将此文件转换为gridview,然后转换为csv格式.我从xml到网格视图都获得了成功.但是我无法进入csv格式.在csv中,它仅显示1到11之间的ID号.因此,请清除我的疑问.在下面,我发送我的xml文件和代码隐藏文件供您验证.请清除我的疑问.
//示例xml//
Hi,
I have a sample XML file. From this I want to convert this file into gridview and then into csv format. I got success from xml to grid view. But I can''t get into csv format. In csv it is displaying only id numbers from 1 to 11. So please clear my doubt. Below I send my xml file and codebehind file for your verification. Please clear my doubt.
//sample xml//
<employee>
<id>1</id>
<name>Florian</name>
<phone>123</phone>
<id>2</id>
<name>Andreas</name>
<phone>234</phone>
<id>3</id>
<name>Martin</name>
<phone>345</phone>
<id>4</id>
<name>jacobs</name>
<phone>111</phone>
<id>5</id>
<name>Ricky</name>
<phone>222</phone>
<id>6</id>
<name>michael</name>
<phone>333</phone>
<id>7</id>
<name>vauchan</name>
<phone>444</phone>
<id>8</id>
<name>shane</name>
<phone>555</phone>
<id>9</id>
<name>watson</name>
<phone>666</phone>
<id>10</id>
<name>Marsh</name>
<phone>777</phone>
</employee>
//以下是文件后面的代码////
//below is the code behind file///
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXml( "C:/Documents and Settings/sridharan/Desktop/sridharan/004_xml_gridview/XMLFile.xml");
GridView1.DataSource =ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Columns[k].HeaderText + '','');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
sb.Append(GridView1.Rows[i].Cells[k].Text + '','');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
推荐答案
嗨
XML结构不会与您显示的代码正确绑定.简单的方法是像这样的结构正确xml.
Hi
The XML structure will not bound correct with the code you are showing. Easy way is correct the xml like this structure.
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<id>1</id>
<name>Florian</name>
<phone>123</phone>
</employee>
<employee>
<id>2</id>
<name>Andreas</name>
<phone>234</phone>
</employee>
</employees>
然后使用这样的gridview标记..
Then have your gridview markup like this..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" />
<asp:BoundField DataField="name" HeaderText="NAME" />
<asp:BoundField DataField="phone" HeaderText="PHONE" />
</Columns>
</asp:GridView>
现在,您的代码将导出所有列
Now your code export all columns
这篇关于如何将xml文件转换为gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!