Completely Free .NET word API

Forums » .NET » .NET »

How can i export xml data to csv file on button click using c# windows form?

Posted Date: 03 Apr 2013      Posted By:: premkumar     Member Level: Silver  Member Rank: 130     Points: 4   Responses: 3

Hi frnds,

I have a windows form i have a button named export to Clcking on that button it should download and save the file to the specified folder.
i need example code in

I need to export those xml data in the form of .csv file on button click...please help me to do this...thanks in advance

My xml data looks like below..

<?xml version="1.0" encoding="utf-8" ?>

Looking forward to hear from you..


Are you looking for a way to export xml data to csv file ? want to download csv file ? then read this thread to learn how to export csv file.


#710290    Author: Prasad kulkarni        Member Level: Gold      Member Rank: 4     Date: 03/Apr/2013   Rating: 2 out of 52 out of 5     Points: 4

First of all you need to read the XML file then you need to create a CSV file and write content in it.

check following code snippet

//create CSV file using stream writer
System.IO.StreamWriter objSW = new System.IO.StreamWriter("Path of CSV Creation");

//read xml file using XMLDocument
XmlDocument objDOM = new XmlDocument();
objDOM.Load("XML file path");
XMLNodeList XNodeList = document.getElementByTagName("user");
for each (XMLNode XN in XNodeList )
//Write content to CSV
objSW.WriteLine(XN.ChildNodes[0].InnerText + "," + XN.ChildNodes[1].InnerText + "," + XN.ChildNodes[2].InnerText);

objSW = null;

Here your file is created successfully.

To download file you can use following link, i have write an article over it.

Hope it helps

Editor, DotNetSpider MVM
Microsoft MVP [ASP.NET/IIS]

#710294    Author: Ram      Member Level: Bronze      Member Rank: 0     Date: 03/Apr/2013   Rating: 2 out of 52 out of 5     Points: 4

protected string ConvertCSVFile(DataSet obj)
string InputXml = obj.GetXml();
string Strcsv = string.Empty;
XDocument docxml = XDocument.Parse(InputXml);
StringBuilder sb = new StringBuilder(100000);
foreach (XElement node in docxml.Descendants("User"))
foreach (XElement item in node.Elements())

sb.AppendFormat("{0},", item.Value);
sb.Remove(sb.Length - 1, 1);
Strcsv = sb.ToString();
Response.ContentType = "text/csv";
Response.ContentEncoding = Encoding.GetEncoding(0);
Response.AppendHeader("Content-Disposition", "attachment;filename=Report.csv");
return Strcsv;

#710311    Author: Bandhavi      Member Level: Silver      Member Rank: 0     Date: 03/Apr/2013   Rating: 2 out of 52 out of 5     Points: 4

Step 1: load XML data. You can copy and paste your XML data to the Source Data box, then click Load button. After you have done so, the Source Data box will turn into a graphical XML viewer and your XML data will be displayed as a node tree.
Step 2: select the XML data you want to convert. Use the graphical XML viewer to navigate the XML node tree, find the node you want and click the radio button. If the seleted node is a simple XML element, only the content of that element will be converted. If the selected the node is a complex XML element, the content of all its child nodes will be converted. If you want to convert the whole XML document, you can select the root node. Click the Convert button, the selected XML data will be converted to a plain text file.
You can repeat Step 2 many times by selecting different nodes of your XML document.
Choose the target file format, CSV or plain text, by clicking Options.
Options You can use the following options to make the converted text in the format you desire.
Field Separator: Field separators will make the converted text easy to read or parse. You can specify how to separate the converted data fields. The default separator is a space. You can change it to any string of characters;
Trim XML Format White Space: Some XML files contain spaces and line breakers for the purpose of formating so that the document can be displayed in a more readable pattern by a text editor. When converted to text, these formating white spaces may not be wanted. You can use this option to trim the unwanted white spaces.
Add Linebreakers to Rows: If you specify to trim formating white spaces, all the data fields will be converted to one long line. You can use this option to add a line breaker to the end of each row of the XML elements.


Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.

  • Return to Discussion Forum
  • Start new thread

  • Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India