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.
-
possible duplicate of Creating a DataTable from CSV Fileshriek– shriek2013-12-25 12:31:12 +00:00Commented Dec 25, 2013 at 12:31
-
no it is not because that example needs file path as a input but in my case it is file contentNilesh– Nilesh2013-12-25 13:19:54 +00:00Commented Dec 25, 2013 at 13:19
-
just to clarify, can you change the way the file is read? like change File.ReadAllText to ReadAllBytes?shriek– shriek2013-12-25 17:00:55 +00:00Commented Dec 25, 2013 at 17:00
Add a comment
|
1 Answer
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.