0

I have string data containing *.csv file content (read from using File.ReadAllText(filePath) method and want create new Data Table object from the string data. I don't have an option to use file path because the file is immediately deleted from the drive once it is been read.

Nilesh
  • 135
  • 1
  • 2
  • 12
  • possible duplicate of [Creating a DataTable from CSV File](http://stackoverflow.com/questions/3306330/creating-a-datatable-from-csv-file) – shriek Dec 25 '13 at 12:31
  • no it is not because that example needs file path as a input but in my case it is file content – Nilesh Dec 25 '13 at 13:19
  • just to clarify, can you change the way the file is read? like change File.ReadAllText to ReadAllBytes? – shriek Dec 25 '13 at 17:00

1 Answers1

0

You could consider replacing your File.ReadAllText method with File.ReadAllLines. Then, you could do something like the below steps:

  1. Create a datatable, define its structure etc.
  2. Read all lines from file as an array of strings
  3. In a loop, split the string by your separator character, then parse each portion of string as its corresponding datatype e.g. int.TryParse() for numerical values etc.
  4. Add new row to the datatable using DataTable.Rows.Add and supplying an object array with your parsed values.
shree.pat18
  • 21,449
  • 3
  • 43
  • 63