Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
---------------------------------------------
C#
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
Updated 17-Aug-12 7:23am
v2

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:

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


Final code:

C#
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);
		}
		
    }
}
 
Share this answer
 
v7
I feel dumb forgot to assign name to datatable....as the error suggested thanks for the help.

C#
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900