i am trying to create a data table from a tab delimited text file.I am getting the values from the file easily.The problem is that when there is a empty column in text file same empty column is not created in data table instead the next non-empty column contents get replaced in the empty columns area
format of data in textfile
id name product cost company name
1 abc shoe xxx
2 xyz chain yyy
Data table obtained
id name product cost company name
1 abc shoe xxx
2 xyz chain yyy
my code to getdata
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);
}