public List <PostsModels> ImportCSV(string postedFile) { try { List <PostsModels> posts = new List <PostsModels>(); string filepath = string.Empty; if (postedFile != null) { //Read csv file. Skip header record List <String> csvData = System.IO.File.ReadAllLines(postedFile).Skip(1).ToList(); //Loop through each row in csv file foreach (string row in csvData) { ArrayList arList = csv.CSVParser(row); posts.Add(new PostsModels { id = Convert.ToInt32(arList[0].ToString()), title = arList[1].ToString(), privacy = arList[2].ToString(), likes = Convert.ToInt32(arList[3].ToString()), views = Convert.ToInt32(arList[4].ToString()), comments = Convert.ToInt32(arList[5].ToString()), timestamp = arList[6].ToString() }); } } return(posts); } catch (Exception ex) { throw ex; } }
public void CSVParserTest() { string inputString = "4841189,\"\"\"Shut Up, Mom!\"\" Disrespectful Kids\",public,87,19542,28,Sun Oct 06 23:44:53 2015"; CSVHelper csv = new CSVHelper(); ArrayList arr = csv.CSVParser(inputString); Assert.AreEqual(arr[1], "\"Shut Up, Mom!\" Disrespectful Kids"); Assert.AreEqual(arr[2], "public"); }
public void CSVParserTest1() { //4840998,"Do You Tell Your Kids to """"Shut Up?""""",public,37,12747,18,Sat Oct 05 21:40:30 2015 string inputString = "4839301,Lower Your Baby's Risk of SIDS With a Fan,private,47,1239,21,Fri Oct 18 04:06:13 2015"; CSVHelper csv = new CSVHelper(); ArrayList arr = csv.CSVParser(inputString); Assert.AreEqual(arr[1], "Lower Your Baby's Risk of SIDS With a Fan"); Assert.AreEqual(arr[6], "Fri Oct 18 04:06:13 2015"); }