Click here to Skip to main content
Sign Up to vote bad
good
See more: C#XMLCSVXmlReader
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


2 solutions

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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 6,889
1 Prasad_Kulkarni 3,671
2 OriginalGriff 3,359
3 _Amy 3,312
4 CPallini 2,925


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