Assuming you have a Test.csv file that you want to read in .Net Application
Value1 , Value2 , Value3
That's look very simple. one line of C# code you can parse this CSV text into three fields.
var fields = line.Split(',');
What if your text line contain COMMA as value, but not the Delimiter?
var fields = line.Split(',');
What if your text line contain COMMA as value, but not the Delimiter?
Value1 , Value2,Value2 , Value3
Emmm!! in this case we just need to wrap DOUBLE QUOTE around the field value, which contain COMMA when we saved the file.
Value1 , "Value2,Value2" , Value3
But still, our C# code still parse it wrong. Because it perform split string operation with COMMA as delimiter. We're gonna need some kind smart CSV Parsing mechanism to solve this problem.
TextFieldParser class come to rescue. you just need to add reference to Microsoft.VisualBasic.dll
TextFieldParser class come to rescue. you just need to add reference to Microsoft.VisualBasic.dll
TextFieldParser parser = new TextFieldParser(@"C:\Test.csv"
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters("," );
while (!parser.EndOfData) {
string[] fields = parser.ReadFields();
}
parser.Close();
);parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters("," );
while (!parser.EndOfData) {
string[] fields = parser.ReadFields();
}
parser.Close();