I am new to c# asp.net
I have a txt file with data
"first","second","third","fourth"
"first","second","third,hai","fourth"
"first","second","third","fourth"
"first","second","third","fourth"
which is a csv file i need to fetch the string from this file and store in database.
You can notice that there is double qoutes and comma and another thing is in 2nd row third column there is a comma in between the whole string .
please help with this.
thanks in advance

Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 5:54 AM EDT
private List ParseCSVFile(string fileNameWithPath) listOfWords = new List();
{
List
try
{
string line;
char[] splitByCharSet = new char[1] { '\"' };
using (StreamReader reader = new StreamReader(fileNameWithPath))
{
while ((line = reader.ReadLine()) != null)
{
string[] items;
//Parsing by firstly double quote character
items = line.Trim().Split(splitByCharSet, StringSplitOptions.RemoveEmptyEntries);
if (items.Length != 0)
{
foreach (string s in items)
{
// Then filtering out standalone comma character and pushing all valid words into a List
if (s != ",")
{
listOfWords.Add(s);
}
}
}
}
}
}
catch (Exception exc)
{
throw exc;
}
return listOfWords;
}
____________________
OUTPUT:
____________________
first
second
third
fourth
first
second
third,hai
fourth
first
second
third
fourth
first
second
third
fourth
VulpesRank #55 · 29.2K reputationPosted Sep 27, 2013, 6:00 AM EDT
The output is:
krishnakumar kuna55 reputationPosted Sep 27, 2013, 6:23 AM EDT
krishnakumar kuna55 reputationPosted Sep 27, 2013, 6:23 AM EDT
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 7:27 AM EDT
krishnakumar kuna55 reputationPosted Sep 27, 2013, 7:31 AM EDT
thanks in advance