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

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.Buffer = true;
       Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
       Response.Charset = "";
       Response.ContentType = "application/text";

       GridView1.AllowPaging = false;

       StringBuilder sb = new StringBuilder();
       for (int k = 0; k < GridView1.Columns.Count; k++)
           //add separator
           sb.Append(GridView1.Columns[k].HeaderText + ',');
       //append new line
       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


Thanks in advance
Updated 17-May-11 21:09pm
Sergey Alexandrovich Kryukov 18-May-11 4:00am    
Web application for doing ***this***?! I can imagine marketing of such "product"...
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.
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