15.09.2019
Posted by 
  1. Turn Csv File Into Xml Files To File

And in most cases the format of the file does not match with the required format of the application you want to use. Therefore you have to convert the file into the right format. There exist many ways to convert data. Within this article, I want to show you how the Windows PowerShell may be used for the conversion of a CSV file into a XML file. This online tool allows you to convert a CSV file into a XML file. To make use of this converter, define a valid XML template using placeholders in the format ##POSITION## to substitute the value of the CSV file within the XML snippet. ##1## is the first value, ##2## the second value and so on. Your template should NOT. How to convert text to xml, convert tab to xml, and convert csv files to xml - Duration: 2:52. HotHotSoftware 24,375 views.

19 Jan 2009CPOL
A small routine to convert a CSV file to a well formatted XML document using LINQ.

Introduction

Recently, I developed a Windows application that received information via a CSV file. I needed to query the information to extract values and make a series of statistic calculations, and obviously, the CSV format was not the ideal way to do them.

I did a small research on the Internet, but I couldn't find any free code to do the job. So, I decided to make it myself, and surprisingly, I discovered a simple way using LINQ to XML.

In this article, I have exposed the code and a small console program to test it. You can use it as you wish. Enjoy it!

Background

Panasonic 3d blu ray player. The method described here converts a CSV file with an undetermined number of rows and fields to a well formatted XML file.

CSV restriction: the CSV file need to have the first row with the name of the fields, as in the following example:

and the routine converts the file to the following XML document:

That is a flat XML file with all fields converted to a value element with the variable name and the variable value. This schema is repeated for each row.

Using the code

The code is simple. Unlike the baroque constructions that DOM need to implement an XML structure, LINQ makes the process surprisingly simple. We use XDocument, the class that LINQ uses to manage the XML InfoSet.

You can see it in the following code:

To use the class, you only need to call the ConvertCsvToXML method with the appropriate parameters. The class is static, and you don't need to create it. The comments are not well formatted, so correct them in your code.

Here is a small test program that converts our CSV example to XML:

This code stores the result in a outputxml.xml file and shows the resulting XML in the console:

Points of interest

This class show us how much we can simplify our programs using LINQ. If you have worked with DOM, you can compare the models and see how the construction of an XML document is drastically simplified by LINQ.

History

  • 19.01.2009 - First version.
Microsoft
Active1 year, 5 months ago

I am looking for a system to convert a spreadsheet (or CSV file) with a few hundred rows of data into individual XML files. One column in the spreadsheet would be the required filename, and the other columns would contain data to be inserted into specific nodes in the XML based on a template.

Kind Regards,Dan

Edit 1If it can be handled totally within Excel, creating a new XML for each row and inserting the relevant column data into the correct location of the XML template that would be the best outcome.

djc123
djc123djc123

3 Answers

Using VBA and the MSXML2.DOMDocument this could be achieved as follows:

Assuming you have a Excel sheet like this:

Then VBA like this should create a XML file for each row:

Axel RichterAxel Richter
29.9k4 gold badges24 silver badges42 bronze badges

I would go the xslt2 path. Save the file as csv, and then you can use a xslt template such as : http://andrewjwelch.com/code/xslt/csv/csv-to-xml_v2.html to convert it to xml.

Edit the template to create a new xsl:result-document whenever you want your new file. Basically you should edit the lines containing

Where a new <row> element is created, and replace them with something like:

Also replace </row> with </xsl:result-document> of-course you should change the template to your output needs, and change <xsl:param name='pathToCSV' select='file:///c:/csv.csv' /> to reference to your file.

The code above is untested, but I believe it should work.

Edit 1:

File

xslt2 bindings exist in many programing languages, and there are stand-alone versions as well. You can also split the process in two: save the xml version in excel, and transform the resulting xml to many xml files.

jarondl

Turn Csv File Into Xml Files To File

jarondl
8202 gold badges12 silver badges23 bronze badges

thanks for the great example. When converting a very large excel spreadsheet with more than 25 rows, I would recommend the following:

Again, thanks for the great example, it did a great job converting very large spreadsheets into XML files suitable from import into other systems.

Robert Moskal
16.1k5 gold badges43 silver badges65 bronze badges
Tony DutcherTony Dutcher

Not the answer you're looking for? Browse other questions tagged xmlexcel or ask your own question.