Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hai,


I am having web application for converting xml to csv.I put One button in my application.If I click that button xml files should be converted into csv.I tried it by using code.But the final output didnot match the xml.Below is my code.Please refer this and give me a idea for how to solve this.

//aspx.cs page
protected void Button1_Click(object sender, EventArgs e)
   {
       DataSet ds = new DataSet();
       ds.ReadXml("C:/Documents and Settings/sridharan/Desktop/sridharan/exporting xml values into excel or csv/products.xml");
       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
<employee>
  
    <id>id</id>
    <name>name</name>
    <phone>phone</phone>
    <address>address</address>
    <age>age</age>
    <dateofbirth>dateofbirth</dateofbirth>
    <company>company</company>
  
  
    <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>

Thanks in advance
Posted
Updated 17-May-11 21:09pm
v2
Comments
Sergey Alexandrovich Kryukov 18-May-11 4:00am    
Web application for doing ***this***?! I can imagine marketing of such "product"...
--SA
Sergey Alexandrovich Kryukov 18-May-11 4:02am    
Except the functionality that hardly can make sense and dirty code, I cannot see what's your problem.
--SA
Legor 18-May-11 5:22am    
What does the csv look like?
sridharan28 18-May-11 5:41am    
There is no text in notepad.

Here[^] is an Archive Article.
 
Share this answer
 
Your code should work, have you tried stepping through the debugger ? You could also work with the XML directly, going through the child nodes at each level.

Your code leaves a , on the end, which is a common issue with solutions like this. One fix is to add the first element, then do a loop to add the others, putting the , BEFORE each element.
 
Share this answer
 
Please check the project XmlToCsv at codeplex. That includes code (nicely isolated from the application) that you can use to do what you ask for. @SAKryukov: "Insults are not welcome".
 
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