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.
Asked
Active
Viewed 506 times
0
-
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 Answers
0
You could consider replacing your File.ReadAllText method with File.ReadAllLines. Then, you could do something like the below steps:
- Create a datatable, define its structure etc.
- Read all lines from file as an array of strings
- 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.
- 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