• How to convert CSV file to XML file in C#

    by  • May 15, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    Comma Separated values or CSV file format is a good way to manage and receive data in an application. It is also called character separated values because a separator character can be different not comma. It is a plain text file and a common file format that is widely used and supported in applications of all technologies. The reason behind the wide use of CSV files is that a developer can easily create a CSV file in code and it can be easily exported to spreadsheet or save in any database. So many applications allow users and provide the facility to export tabular data into CSV file. On the other hand, XML is a markup language that defines a set of rules to define data in a format that can be readable both for human and machine. In XML, tags are not predefined. We need to define it according to our data so XML is designed to be self-descriptive. HTML was designed to display data while XML was designed to transport and save data.

    Sometimes we need to convert our CSV file data into XML file data so that we can use it for our purpose. This is very simple in ASP.NET as we have number of solutions available for this. One simple way is to use LINQ and convert user CSV file into XML. If you are not familiar with LINQ then no need to worry. Just see my code, get some help and use it according to user need.  Furthermore, I will also write article on LINQ to clear the concept. For now, enjoy the simple way of generating XML file from CSV file using LINQ in C#.

    First see what data our CSV file contains

    You need to use following namespaces in your code

    Now write below code where you want to generate XML

    Now see how the output XML file looks like


    Best WordPress Themes and Plugins with Great Team and Support!

    Avatar of GetCodeSnippet.com


    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply