Click here to Skip to main content
12,351,822 members (32,092 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML CSV XmlReader
I am very new to C# programming. I have a .csv with 7 Columns. The columns headings output to xml but the row data does not. The data is tab delimited. Where is my code broken to not output the row data? I am using monodevelop on an Ubuntu 12.04 system. Thank you so much.

Marc
---------------------------------------------
using System.Data;
using System.IO;
 
namespace CSVTOXML
{
	class Program
	{
        	static void Main(string[] args)
        	{
            		//change the filepath to your filepath
            		string filepath = @"myinputfile.csv";
            		StreamReader reader = new StreamReader(filepath);
            		string[] headers = reader.ReadLine().Split('\t'); //grabbing the column names

			//create a table in memory to hold the values until you submit them
			DataTable table = new DataTable();
            		foreach (string header in headers)
            		{
                		table.Columns.Add(header);
            		}
            		string[] data = new string[headers.Length-1]; //NUMBER OF COLUMNS
            		while (!reader.EndOfStream)
            		{
                		data = reader.ReadLine().Split('\t'); //grab a line from the csv and split the contents
                		DataRow row = table.NewRow();
                		for (int i = 0; i < data.Length-1; i++) //add the data from the file to datatable in memory
                		{
                    			row[i] = data[i];
                		}
                		table.Rows.Add(row);
            		}
            		//write data table to xml
            		table.WriteXml("myoutputXML.xml",XmlWriteMode.WriteSchema, true);
		}
	}
}
Posted 17-Aug-12 7:16am
Edited 17-Aug-12 7:23am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Have you tried to debug it? This is a quite simple code, debugging it might reveal, what is wrong.
You have to set table.TableName = "SomeTableName";
and use this:

while (!reader.EndOfStream) 
   {
   table.Rows.Add(reader.ReadLine().Split('\t'));
   }

Final code:

using System.IO;
using System.Data;
 
namespace CSVTOXML
{
	class Program
    {
		static void Main(string[] args)
        	{
            	string filepath = @"inputfile.csv";
            	StreamReader reader = new StreamReader(filepath);
            	string[] headers = reader.ReadLine().Split('\t');
 
		DataTable table = new DataTable();
		table.TableName = "MyTable";
            	foreach (string header in headers)
            	{
                   table.Columns.Add(header);
            	}
            	while (!reader.EndOfStream)
            	{
            	   table.Rows.Add(reader.ReadLine().Split('\t'));
            	}
            		
            	table.WriteXml("myoutputXML.xml",XmlWriteMode.WriteSchema, true);
		}
		
    }
}
  Permalink  
v7
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I feel dumb forgot to assign name to datatable....as the error suggested thanks for the help.

dt.TableName = "myTable";
 
dt.WriteXml(@"path", true);

Debug error is--Unhandled Exception: System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set. Fixed the -1 error in array thank you for that. Not sure why I did it.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100