I have some pretty typical code to parse a CSV file using a Microsoft.VisualBasic.FileIO.TextFieldParser
:
using (TextFieldParser parser = new TextFieldParser(new StringReader(jobsReport)))
{
parser.SetDelimiters(",");
parser.HasFieldsEnclosedInQuotes = true;
string[] headers = parser.ReadFields();
while (!parser.EndOfData)
{
// ERROR OCCURS HERE
string[] fields = parser.ReadFields();
// ...
}
}
However, I get the following exception:
Microsoft.VisualBasic.FileIO.MalformedLineException: Line 195 cannot be parsed using the current Delimiters.
Line 195 (according to Notepad++) is as follows:
0,0,0,0,0,0,0,2017-01-03T00:00:00,,"72d7a7e9-8700-4014-916c-a85e9a6b1ac5",1,REF212U,CREATED,"Evan job for ""Publish Bug""",Changzhou,China,31.77359,119.95401,Jiangsu
If I manually get rid of the double quotes and make the line this:
0,0,0,0,0,0,0,2017-01-03T00:00:00,,"72d7a7e9-8700-4014-916c-a85e9a6b1ac5",1,REF212U,CREATED,"Evan job for Publish Bug",Changzhou,China,31.77359,119.95401,Jiangsu
it works perfectly. I'm confused as to why this causes an issue, though, because according to this Q&A, this is the correct way to escape double quotes in a CSV file.
Also, Excel does just fine with this file:
This is exactly how I'd expect this to be handled.
Microsoft has a document that describes the error as follows:
The specified line cannot be parsed because it uses delimiters other than those specified.
It then rather unhelpfully suggests that I change the following to fix it:
Adjust Delimiters so the line can be parsed correctly, or insert exception-handling code in order to handle the line.
I don't understand how this applies, though - that doesn't appear to be the problem in this case because I was able to fix the problem by removing the double quotes inside the cell, not by changing anything about the delimiters.
My questions, then: why does the parser crash on this line, even though the escaping appears to be correct? And why this particular error message? And, of course, most importantly: how do I fix this?
Edit: Here are a few more of the surrounding lines:
"RBCD","0","0","0","1","0","4","0","2017-01-02T00:00:00","","f233e70a-293d-4953-b96d-79eb29261ea7","1","REF211I","OFFER","Planning Engineer","Wuxi","China"
"AFCO","0","0","0","0","0","0","0","2017-01-03T00:00:00","","15b6eda6-ce71-426d-8530-49c9044b3d62","0","REF214Q","CREATED","Technical Consultant","Shanghai","China"
"RBUS","0","0","0","0","0","0","0","2017-01-03T00:00:00","","a8f3930f-897c-4e4d-8b52-9029efdb9c65","0","REF215E","CREATED","Sr. Calibration Engineer","Farmington Hills","United States"
"PTCN","0","0","0","0","0","0","0","2017-01-03T00:00:00","","72d7a7e9-8700-4014-916c-a85e9a6b1ac5","1","REF212U","CREATED","Evan job for ""Publish Bug""","Changzhou","China"
"RBAC","0","0","0","0","0","0","0","2017-01-03T00:00:00","","fd643834-bafd-4674-aad9-4fe5cb0271cd","0","REF213F","CREATED","Test_Technical Consultant","Suzhou","China"
"RBCN","0","0","0","0","0","1","0","2017-01-04T00:00:00","","0c450e80-f64e-429d-9d85-3fd09a806d1f","1","REF218R","SOURCING","Test Job Role approver","Shanghai","China"
"","0","0","0","1","0","3","0","2017-01-04T00:00:00","","c952f49c-db78-4a86-8ad5-797e1b7f6933","1","REF217C","OFFER","quality engineer-SQE","Shanghai","China"