CSV到XML我的输出XML具有列标题,但行中没有数据 [英] CSV to XML my output XML has the column heading but no data from the rows

查看:143
本文介绍了CSV到XML我的输出XML具有列标题,但行中没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#编程非常陌生.我有一个具有7列的.csv.列标题输出到xml,但行数据不输出.数据是制表符分隔的.我的代码在哪里坏了,不输出行数据?我在Ubuntu 12.04系统上使用monodevelop.非常感谢.

马克
--------------------------------------------- <小>

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);
		}
	}
}

推荐答案

您是否尝试过调试?这是一个非常简单的代码,调试它可能会发现什么地方出了问题.
您必须设置table.TableName = "SomeTableName";
并使用它:

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);
		}
		
    }
}


我觉得自己很愚蠢,忘记为数据表分配名称了.
I feel dumb forgot to assign name to datatable....as the error suggested thanks for the help.

dt.TableName = "myTable";

dt.WriteXml(@"path", true);



调试错误是-未处理的异常:System.InvalidOperationException:无法序列化DataTable.未设置数据表名称.修复了数组-1错误,谢谢.不知道为什么要这么做.



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.


这篇关于CSV到XML我的输出XML具有列标题,但行中没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆