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 SrivastavaPosted Sep 27, 2013, 9:54 AM
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
krishnakumar kunaPosted Sep 27, 2013, 11:31 AM
thanks in advance
Hemant SrivastavaPosted Sep 27, 2013, 11:27 AM
krishnakumar kunaPosted Sep 27, 2013, 10:23 AM
krishnakumar kunaPosted Sep 27, 2013, 10:23 AM
VulpesPosted Sep 27, 2013, 10:00 AM
The output is: