CodexBloom - Programming Q&A Platform

Parsing Custom CSV with Embedded Quotes in C# - Unexpected Line Breaks

👀 Views: 13 đŸ’Ŧ Answers: 1 📅 Created: 2025-08-21
c# csv parsing textfieldparser C#

I'm writing unit tests and I'm having a tough time parsing a custom CSV file in C# that includes fields with embedded quotes and line breaks. The CSV follows a non-standard format where fields can contain line breaks if they are enclosed in double quotes. For example: ``` "Name, Age, City" "John Doe, 30, "New York, NY" "Jane Smith, 25, "Los Angeles, CA" "Alice, 28, "San Francisco, CA" ``` When I try to parse this using the `TextFieldParser` from the `Microsoft.VisualBasic.FileIO` namespace, it fails to handle the embedded quotes correctly, resulting in an `InvalidOperationException`. Here's the code snippet I'm using: ```csharp using Microsoft.VisualBasic.FileIO; public void ParseCustomCSV(string filePath) { using (TextFieldParser parser = new TextFieldParser(filePath)) { parser.SetDelimiters(","); parser.HasFieldsEnclosedInQuotes = true; while (!parser.EndOfData) { string[] fields = parser.ReadFields(); // Processing fields... } } } ``` Despite setting `HasFieldsEnclosedInQuotes` to true, I'm seeing the following behavior: ``` Unhandled Exception: System.InvalidOperationException: Field ends unexpectedly. ``` I've tried various combinations of settings and used a few different parsers, but they all seem to struggle with this format. I've also experimented with regular expressions but found it difficult to maintain performance and readability. Is there a recommended approach to correctly handle this situation, or do I need to preprocess the CSV file to escape the quotes before parsing? Any guidance would be greatly appreciated! I'm working in a CentOS environment.