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: 1252 Points: 4
I have a windows form i have a button named export to csv.by Clcking on that button it should download and save the file to the specified folder.
i need example code in c#.net.
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: Diamond Member Rank: 7 Date: 03/Apr/2013 Rating: 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.InnerText + "," + XN.ChildNodes.InnerText + "," + XN.ChildNodes.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: 2012 Date: 03/Apr/2013 Rating: 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.Remove(sb.Length - 1, 1);
Strcsv = sb.ToString();
Response.ContentType = "text/csv";
Response.ContentEncoding = Encoding.GetEncoding(0);
|#710311 Author: Bandhavi Member Level: Silver Member Rank: 309 Date: 03/Apr/2013 Rating: 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.
| ||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.
Active MembersTodayLast 7 Daysmore...
Talk to Webmaster Tony John