title: CSV Text file parser with TextFieldParser - MalformedLineException
body: <p>I am working on a CSV parser using C# <a href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser%28v=vs.100%29.aspx" rel="noreferrer">TextFieldParser</a> class.</p>
  
  <p>My CSV data is deliminated by <code>,</code> and the string is enclosed by a <code>"</code> character.</p>
  
  <p>However, sometimes the data row cell can also have a <code>"</code> which appears to be making the parser throw an exception.</p>
  
  <p><img src="https://i.stack.imgur.com/dZC1c.png" alt="enter image description here"></p>
  
  <p>This is my C# code so far:</p>
  
  <pre><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
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This is the content of my <code>test.csv</code> file:</p>
  
  <pre><code>" dummy test"s data",   b  ,  c  
  d,e,f
  gh,ij
  </code></pre>
  
  <p>What is the best way to deal with <code>"</code> in my row cell data?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Based on <code>Tim Schmelter's</code> answer, I have modified my code to the following:</p>
  
  <pre><code>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 =&gt; f.Trim(new[] { ' ', '"' }));
  
              Console.WriteLine(String.Join(" | ", cleanFieldRowCells));
          }
      }
  }
  </code></pre>
  
  <p>Which appears to produce the following (correctly):</p>
  
  <p><img src="https://i.stack.imgur.com/Ggi4f.png" alt="enter image description here"></p>
  
  <p>Is this is the best way to deal with string enclosed by quotes, having quotes?</p>