I need to convert a csv file(size is 60 mb) to xml file in my application developed in C#.

I would like to convert .csv file of size 60 mb  into xml file.

So, how can i do it in c# code.

Could you send sample code to get it work.

Thanks in advance.

jella
vishwanadhamjellaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InteractiveMindCommented:
Well, if you're happy to use any method to get the job done, then here's an existing program which does it for you (it's in Java mind you):
http://www.filehungry.com/english/product/windows_software/utilities/file_converters/my_csv_to_xml_converter


Alternatively, read each line in the CSV file into a string, use the Split() method to tokenize the string (with the delimiter set to ","), and then use the XmlTextWriter class to write the data to an XML file:
http://www.filehungry.com/english/product/windows_software/utilities/file_converters/my_csv_to_xml_converter
InteractiveMindCommented:
Sorry, clearly the URL for the XmlTextWriter class had not been copied to my clipboard properly; here it is:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmltextwriterclasstopic.asp
blackbrocoliCommented:
using System;
using System.IO;
using System.Xml;

namespace CsvToXmlSample
{
      class Program
      {
            [STAThread]
            static void Main(string[] args)
            {
                  //Creates the XML writer to write XML content
                  using(XmlTextWriter xmlWriter = new XmlTextWriter("c:\\output.xml", System.Text.Encoding.UTF8))
                  {
                        //Starts the XML document
                        xmlWriter.WriteStartDocument();
                        //Writes the root element
                        xmlWriter.WriteStartElement("csv");
                        
                        //Opens the input file
                        using(StreamReader inputFile = new StreamReader("c:\\input.csv"))
                        {
                              //Loop on each line in the file
                              while(inputFile.Peek() > -1)
                              {
                                    //Reads the line
                                    string row = inputFile.ReadLine();
                                    //Splits each field
                                    string[] parts = row.Split(';');

                                    //Writes a record element
                                    xmlWriter.WriteStartElement("record");

                                    //Writes the first field (named field1)
                                    xmlWriter.WriteElementString("field1", parts[0]);
                                    //Writes the second field (named field2)
                                    xmlWriter.WriteElementString("field2", parts[1]);
                                    //Writes the third field (named field3)
                                    xmlWriter.WriteElementString("field3", parts[2]);
                                    ...

                                    //Ends the record element
                                    xmlWriter.WriteEndElement();
                              }      

                              //Ends the root element
                              xmlWriter.WriteEndElement();
                              //Ends the document
                              xmlWriter.WriteEndDocument();
                        }
                  }
            }
      }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.