CSV Text File Parser with TextFieldParser - MalformedLineException

I am working on a CSV parser using the C # TextFieldParser class.

My CSV data is divided by,, and the string is enclosed in the character " .

However, sometimes the data row cell may also have " , which apparently causes the parser to throw an exception.

enter image description here

This is my C # code:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using Microsoft.VisualBasic.FileIO; namespace CSV_Parser { class Program { static void Main(string[] args) { // Init string CSV_File = "test.csv"; // Proceed If File Is Found if (File.Exists(CSV_File)) { // Test Parse_CSV(CSV_File); } // Finished Console.WriteLine("Press any to exit ..."); Console.ReadKey(); } static void Parse_CSV(String Filename) { using (TextFieldParser parser = new TextFieldParser(Filename)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); parser.TrimWhiteSpace = true; while (!parser.EndOfData) { string[] fieldRow = parser.ReadFields(); foreach (string fieldRowCell in fieldRow) { // todo } } } } } } 

This is the contents of my test.csv file:

 " dummy test"s data", b , cd,e,f gh,ij 

What is the best way to work with " in my row cell data?


UPDATE

Based on Tim Schmelter's answer, I changed my code to the following:

 static void Parse_CSV(String Filename) { using (TextFieldParser parser = new TextFieldParser(Filename)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); parser.HasFieldsEnclosedInQuotes = false; parser.TrimWhiteSpace = true; while (parser.PeekChars(1) != null) { var cleanFieldRowCells = parser.ReadFields().Select( f => f.Trim(new[] { ' ', '"' })); Console.WriteLine(String.Join(" | ", cleanFieldRowCells)); } } } 

It seems that the following is being created (correctly):

enter image description here

Is this the best way to handle quoted string with quotation marks?

+6
source share
1 answer

Could you omit the quote character by setting HasFieldsEnclosedInQuotes to false ?

 using (var parser = new TextFieldParser(@"Path")) { parser.HasFieldsEnclosedInQuotes = false; parser.Delimiters = new[]{","}; while(parser.PeekChars(1) != null) { string[] fields = parser.ReadFields(); } } 

You can remove quotes manually:

 var cleanFields = fields.Select(f => f.Trim(new[]{ ' ', '"' })); 
+3
source

All Articles