Click here to Skip to main content
15,916,601 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,


I got lot of responses for my file conversion project.It is very useful for me.But at the same time i got the error while converting xml to csv.After running my project the file is in excel format but not csv.Then I am getting the error for this xml to csv.plz clear it.Below i had attached the code.I am also willing to edit that xml file.plz help me.
//default.aspx.cs//
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;


public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         DataSet ds=new DataSet();
        ds.ReadXml( "C:/Documents and Settings/sridharan/Desktop/sridharan/exporting               
        xml values into excel or csv/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=gurunanak.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 + ',');
        }
       
        sb.Append("\r\n");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
              
                sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
            }
            //append new line
            sb.Append("\r\n");
        }
        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind(); 
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
       
    }

   
}


//default.aspx//

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
            Style="left: 124px; position: relative; top: 21px" AutoGenerateColumns="False" 
           
           >
            <footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White" />
            <rowstyle backcolor="Aqua" forecolor="Fuchsia" />
            <editrowstyle backcolor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <pagerstyle backcolor="#284775" forecolor="White" horizontalalign="Center" />
            <HeaderStyle BackColor="#FF9900" Font-Bold="True" ForeColor="#CC00FF" />
            <alternatingrowstyle backcolor="White" forecolor="#284775" />
            <columns>
                <asp:BoundField HeaderText="id" DataField="id" />
                <asp:BoundField HeaderText="name" 
                    DataField="name" ItemStyle-HorizontalAlign="Right" >
                 
<itemstyle horizontalalign="Right"></itemstyle>
                
                   <asp:BoundField HeaderText="phone" DataField="phone" />
                    <asp:BoundField HeaderText="address" DataField="address" />
                     <asp:BoundField HeaderText="age" DataField="age" />
                      <asp:BoundField HeaderText="dateofbirth" DataField="dateofbirth" />
                       <asp:BoundField HeaderText="company" DataField="company" />
            </columns> 
        
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    
    </form>
</body>
</html>

//xmlfile.xml
XML
<employee>
  
    <id>1</id>
    <name>Florian</name>
    <phone>123</phone>
    <address>mumbai</address>
    <age>32</age>
    <dateofbirth>8.8.78</dateofbirth>
    <company>skysoft</company>
    
  
  
    <id>2</id>
    <name>Andreas</name>
    <phone>234</phone>
    <address>bengaluru</address>
    <age>21</age>
    <dateofbirth>7.7.85</dateofbirth>
    <company>Infotechsolutions</company>
  
  
    <id>3</id>
    <name>Martin</name>
    <phone>345</phone>
    <address>hyderabad</address>
    <age>47</age>
    <dateofbirth>29.3.87</dateofbirth>
    <company>vayana</company>
  
  
    <id>4</id>
    <name>jacobs</name>
    <phone>111</phone>
    <address>kolkatta</address>
    <age>22</age>
    <dateofbirth>2.2.88</dateofbirth>
    <company>brainmagic</company>
  
  
    <id>5</id>
    <name>Ricky</name>
    <phone>222</phone>
    <address>trichy</address>
    <age>55</age>
    <dateofbirth>4.6.99</dateofbirth>
    <company>Infosys</company>
  
  
    <id>6</id>
    <name>michael</name>
    <phone>333</phone>
    <address>chennai</address>
    <age>28</age>
    <dateofbirth>31.7.91</dateofbirth>
    <company>Photon</company>
  
  
    <id>7</id>
    <name>vauchan</name>
    <phone>444</phone>
    <address>chennai</address>
    <age>55</age>
    <dateofbirth>5.5.77</dateofbirth>
    <company>OFS</company>
  
  
    <id>8</id>
    <name>shane</name>
    <phone>555</phone>
    <address>chennai</address>
    <age>33</age>
    <dateofbirth>23.5.67</dateofbirth>
    <company>Sunsmart</company>
  
  
    <id>9</id>
    <name>watson</name>
    <phone>666</phone>
    <address>chennai</address>
    <age>44</age>
    <dateofbirth>18.9.85</dateofbirth>
    <company>Ascendas</company>
  
  
    <id>10</id>
    <name>Marsh</name>
    <phone>777</phone>
    <address>chennai</address>
    <age>22</age>
    <dateofbirth>1.1.1</dateofbirth>
    <company>sap</company>
  
  
    <id>11</id>
    <name>sridharan</name>
    <phone>888</phone>
    <address>chennai</address>
    <age>31</age>
    <dateofbirth>22.4.66</dateofbirth>
    <company>Wipro</company>
  
  
    <id>12</id>
    <name>mishra</name>
    <phone>999</phone>
    <address>Delhi</address>
    <age>23</age>
    <dateofbirth>1.1.99</dateofbirth>
    <company>Infosys</company>
  
  
    <id>13</id>
    <name>vaibhav</name>
    <phone>1000</phone>
    <address>Ranchi</address>
    <age>22</age>
    <dateofbirth>11.2.98</dateofbirth>
    <company>TCS</company>
  
  
    <id>14</id>
    <name>Rajesh</name>
    <phone>1100</phone>
    <address>kochin</address>
    <age>11</age>
    <dateofbirth>29.3.78</dateofbirth>
    <company>CTS</company>
  
</employee>
Posted
Updated 17-Mar-11 1:13am
v2

1 solution

hi
YOu can follow the below link for the solution..

Convert XML to CSV, with XSL[^]


Regards
Ravi
 
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