Read text file and convert to Data table using swagger web api
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Nov 2, 2022, 4:40 AM EDT
please use below code
public DataTable GetDataSourceFromFile(string fileName) { DataTable dt = new DataTable("CreditCards"); string[] columns = null; var lines = File.ReadAllLines(fileName); // assuming the first row contains the columns information if (lines.Count() > 0) { columns = lines[0].Split(new char[] { ',' }); foreach (var column in columns) dt.Columns.Add(column); } // reading rest of the data for (int i = 1; i < lines.Count(); i++) { DataRow dr = dt.NewRow(); string[] values = lines[i].Split(new char[] { ',' }); for (int j = 0; j < values.Count() && j < columns.Count(); j++) dr[j] = values[j]; dt.Rows.Add(dr); } return dt; }krishna Theliwala143 reputationPosted Nov 2, 2022, 11:35 PM EDT
Please check attached text file.
Not a fixed charcter in that files and not used delimeter