Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
    }
Posted
Updated 3-Mar-11 22:08pm
v2

1 solution

Hi

The XML structure will not bound correct with the code you are showing. Easy way is correct the xml like this structure.

XML
<?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>


Then have your gridview markup like this..

XML
<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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900