I am using File Helper library in my .net core project to Parse CSV file to a class and i am getting error while doing so because one the field in the file have comma seperated value so i am unable parse it.
Here is the value in the file
**Branch,ANumber,Type,PNumber,CustNumber,Reference,MemberName,Code,IncidentDate
XYZ,15555,New,4545454545,8998-42454-1215,S454545/112,"Newmon, Osborn",GGG SHF,28/01/2022**
here the field value Newmon, Osborn is the trouble
var filePath = @"File.csv";
List<string> lines = new List<string>(System.IO.File.ReadAllLines(filePath));
var data = testSubject.Parse(lines).FirstOrDefault();
Assert.Equal("XYZ",data.Branch);
Assert.Equal("15555",data .ANumber);
Assert.Equal("New",data.Type);
Assert.Equal("4545454545",data.PNumber);
Assert.Equal("899-42-12154",data.CustNumber);
Assert.Equal("S454545/112",data.Reference);
Assert.Equal("Newmon, Osborn",data.MemberName);
Assert.Equal("GGG SHF",data.Code);
Assert.Equal(DateTime.ParseExact("28/01/2022", "dd/MM/yyyy", null),data.IncidentDate);
Here is my class
[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class ABC
{
[FieldNullValue(typeof(string), "")]
public string Branch{ get; set; }
[FieldNullValue(typeof(string), "")]
public string ANumber{ get; set; }
[FieldNullValue(typeof(string), "")]
public string Type{ get; set; }
[FieldNullValue(typeof(string), "")]
public string PNumber{ get; set; }
[FieldNullValue(typeof(string), "")]
public string CustNumber{ get; set; }
[FieldNullValue(typeof(string), "")]
public string Reference{ get; set; }
[FieldNullValue(typeof(string), "")]
public string MemberName{ get; set; }
[FieldNullValue(typeof(string), "")]
public string Code{ get; set; }
[FieldNullValue(typeof(DateTime), "1900-01-01"), FieldConverter(ConverterKind.DateMultiFormat, "dd/MM/yyyy", "yyyy-MM-dd", "MM/dd/yyyy")]
public DateTime IncidentDate{ get; set; }
}
While validating the values with the parsing engine with following code i am getting the error
ParsingEngine.ReadStringAsList(line);
Can anybody help me in this situation. thanks in advance.
The library you're referring to is probably FileHelpers.net instead of 'File Helper'. At first glance it doesn't seem to be able to work with text-qualified CSV fields. Maybe you could use the Multiple Delimiters option to split on quote first, then comma?
Otherwise you'll need a more sophisticated CSV library like CsvHelper or my own library