Hi All,
I have a CSV file which has 7 columns which are separated by commas. One of the columns is speical since it has data enclosed by double quotes and has newlines separating each entry for that column. So for examples sake it looks like this;
thefirstpieceofdata,"A
B
C
D
E",somemoredata
Using the following code I have for parsing CSV files works fine until it comes up against the speical column;
static DataTable GetDataTableFromCsv(string path) { string[] Lines = System.IO.File.ReadAllLines(path); string[] Fields; Fields = Lines[0].Split(new char[] { ',' }); int Cols = Fields.GetLength(0); DataTable dt = new DataTable(); //1st row must be column names; force lower case to ensure matching later on. for (int i = 0; i < Cols; i++) dt.Columns.Add(Fields[i], typeof(string)); DataRow Row; for (int i = 1; i < Lines.GetLength(0); i++) { Fields = Lines[i].Split(new char[] { ',' }); Row = dt.NewRow(); for (int f = 0; f < Cols; f++) Row[f] = Fields[f]; dt.Rows.Add(Row); } return dt; }
How can I handle this in the above code so it actually returns the correct datatable which I expect?