Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制表符分隔的文本文件中出现空列问题_C#_Asp.net_Datatable_Text Files - Fatal编程技术网

C# 制表符分隔的文本文件中出现空列问题

C# 制表符分隔的文本文件中出现空列问题,c#,asp.net,datatable,text-files,C#,Asp.net,Datatable,Text Files,我正在尝试从制表符分隔的文本文件创建数据表。我很容易从该文件中获取值。问题是,当文本文件中有空列时,不会在数据表中创建相同的空列,而是在空列区域中替换下一个非空列内容 文本文件中的数据格式 id name product cost company name 1 abc shoe xxx 2 xyz chain yyy 获得的数据表 id name pr

我正在尝试从制表符分隔的文本文件创建数据表。我很容易从该文件中获取值。问题是,当文本文件中有空列时,不会在数据表中创建相同的空列,而是在空列区域中替换下一个非空列内容

文本文件中的数据格式

id    name    product    cost     company name


1      abc    shoe                   xxx
2      xyz    chain                  yyy
获得的数据表

id    name    product    cost     company name

1      abc     shoe       xxx
2      xyz     chain      yyy
获取数据的代码

var reader = new StreamReader(File.OpenRead(@"d:\d.txt"));
        var table = new DataTable("SampleTable");
        string[] fieldValues = reader.ReadLine().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < fieldValues.Length; i++)
        {
            table.Columns.Add(new DataColumn(fieldValues[i].ToString().Trim()));
        }
        while (!reader.EndOfStream)
        {

            var line = reader.ReadLine().Trim();
            var values = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
            string[] ee = values;

            var newRow = table.NewRow();
            for (int i = 0; i < ee.Length; i++)
            {
                newRow[i] = ee[i].Trim().ToString(); // like sample [0,0]
            }
            table.Rows.Add(newRow);
        }
var reader=newstreamreader(File.OpenRead(@“d:\d.txt”);
var表=新数据表(“样本表”);
string[]fieldValues=reader.ReadLine().Split(新字符[]{'\t'},StringSplitOptions.RemoveEmptyEntries);
for(int i=0;i
如果有空列,则应读取空字符串,而不是空字符串

换句话说

1,abc,鞋,xxx


获取结果的原因如下:StringSplitOptions.RemoveEmptyEntries

您告诉
Split
通过设置选项
StringSplitOptions来精确执行您观察到的操作。RemoveEmptyEntries
-它删除空条目


删除此选项,它将保留空列。

问题可能是您使用
removeMptyEntries
选项拆分了行:

var values = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
空单元格被删除。忽略此参数,它应该可以工作