Advertisement
nex036ara

read_csv_c#

Oct 31st, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace StringMatching
  8. {
  9.    
  10.     public class CsvRow : List<string>
  11.     {
  12.         public string LineText { get; set; }
  13.     }
  14.  
  15.     class CsvFileReader : StreamReader
  16.     {
  17.         public CsvFileReader(Stream stream)
  18.             : base(stream)
  19.         {
  20.         }
  21.  
  22.         public CsvFileReader(string filename)
  23.             : base(filename)
  24.         {
  25.         }
  26.  
  27.  
  28.          /// <summary>
  29.         /// Reads a row of data from a CSV file
  30.         /// </summary>
  31.         /// <param name="row"></param>
  32.         /// <returns></returns>
  33.         public bool ReadRow(CsvRow row)
  34.         {
  35.             row.LineText = ReadLine();
  36.             if (String.IsNullOrEmpty(row.LineText))
  37.                 return false;
  38.  
  39.             int pos = 0;
  40.             int rows = 0;
  41.  
  42.             while (pos < row.LineText.Length)
  43.             {
  44.                 string value;
  45.  
  46.                 // Special handling for quoted field
  47.                 if (row.LineText[pos] == '"')
  48.                 {
  49.                     // Skip initial quote
  50.                     pos++;
  51.  
  52.                     // Parse quoted value
  53.                     int start = pos;
  54.                     while (pos < row.LineText.Length)
  55.                     {
  56.                         // Test for quote character
  57.                         if (row.LineText[pos] == '"')
  58.                         {
  59.                             // Found one
  60.                             pos++;
  61.  
  62.                             // If two quotes together, keep one
  63.                             // Otherwise, indicates end of value
  64.                             if (pos >= row.LineText.Length || row.LineText[pos] != '"')
  65.                             {
  66.                                 pos--;
  67.                                 break;
  68.                             }
  69.                         }
  70.                         pos++;
  71.                     }
  72.                     value = row.LineText.Substring(start, pos - start);
  73.                     value = value.Replace("\"\"", "\"");
  74.                 }
  75.                 else
  76.                 {
  77.                     // Parse unquoted value
  78.                     int start = pos;
  79.                     while (pos < row.LineText.Length && row.LineText[pos] != ',')
  80.                         pos++;
  81.                     value = row.LineText.Substring(start, pos - start);
  82.                 }
  83.  
  84.                 // Add field to list
  85.                 if (rows < row.Count)
  86.                     row[rows] = value;
  87.                 else
  88.                     row.Add(value);
  89.                 rows++;
  90.  
  91.                 // Eat up to and including next comma
  92.                 while (pos < row.LineText.Length && row.LineText[pos] != ',')
  93.                     pos++;
  94.                 if (pos < row.LineText.Length)
  95.                     pos++;
  96.             }
  97.             // Delete any unused items
  98.             while (row.Count > rows)
  99.                 row.RemoveAt(rows);
  100.  
  101.             // Return true if any columns read
  102.             return (row.Count > 0);
  103.         }
  104.     }
  105.  
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement