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.

3
  • possible duplicate of Creating a DataTable from CSV File Commented 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 content Commented Dec 25, 2013 at 13:19
  • just to clarify, can you change the way the file is read? like change File.ReadAllText to ReadAllBytes? Commented Dec 25, 2013 at 17:00

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.