c# - Ignoring CSV rows with no data -


i'm surprised haven't seen on here (or maybe missed it). when parsing csv file, if there rows no data, how can/should handled? i'm not talking blank rows, empty rows, example:

id,name,quantity,price 1,stuff,2,5 2,things,1,2.5 ,,, ,,, ,,, 

i using textfieldparser handle commas in data, multiple delimiters, etc. 2 solutions i've thought of either use readline instead of readfields, remove benefits of using textfieldparser, i'd assume, because i'd have handle commas different way. other option iterate through fields , drop row if of fields empty. here's have:

dttexceltable = new datatable();  using (textfieldparser parser = new textfieldparser(filename)) {     parser.delimiters = new string[] { ",", "|" };      string[] fields = parser.readfields();     if (fields == null)     {        return null;     }     foreach (string columnheader in fields)     {        dttexceltable.columns.add(columnheader);     }      while (true)     {         datarow importedrow = dttexceltable.newrow();         fields = parser.readfields();         if (fields == null)         {             break;         }         (int = 0; < fields.length; i++)         {             importedrow[i] = fields[i];         }          foreach (var field in importedrow.itemarray)         {             if (!string.isnullorempty(field.tostring()))             {                 dttexceltable.rows.add(importedrow);                 break;             }         }      } } 

without using thirdy party csv reader change code in way

..... datarow importedrow = dttexceltable.newrow(); (int = 0; < fields.length; i++)     importedrow[i] = fields[i];  if(!importedrow.itemarray.all (ia => string.isnullorwhitespace(ia.tostring())))      dttexceltable.rows.add(importedrow); 

using ienumerable extension check every element of itemarray using string.isnullorwhitespace. if return true have array of empty string , skip add


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -