Search code examples
c#csvstreamreader

Best way StreamReader skipping Null or WhiteSpace line


After searching and trying the different ways I found I either wasn't happy with the way I was doing the code or it didn't work right for me. I'm new at programming so my understanding is limited. Please keep in mind with the answer.

I want to read a .csv file line by line and skipping lines that are blank. With the contents of the lines I want to put into a list of object. I have everything working except for the skipping line part. Also any feedback about improving any parts of my code are all welcome. I like constructive criticism.

public void CardaxCsvFileReader()
    {
        string cardaxCsvPath = (@"C:\Cardax2WkbTest\Cardax\CardaxTable.csv");

        try
        {
            using (System.IO.StreamReader cardaxSR =
                new System.IO.StreamReader(System.IO.File.OpenRead(cardaxCsvPath)))
            {
                string line = "";
                string[] value = line.Split(',');

                while (!cardaxSR.EndOfStream)
                {                              // this commented out part is what I would like to work but doesn't seem to work.
                    line = cardaxSR.ReadLine();//.Skip(1).Where(item => !String.IsNullOrWhiteSpace(item));
                    value = line.Split(',');

                    if (line != ",,,,,") // using this as temp to skip the line because the above commented out part doesn't work.
                    {
                    CardaxDataObject cardaxCsvTest2 = new CardaxDataObject();

                    cardaxCsvTest2.EventID = Convert.ToInt32(value[0]);
                    cardaxCsvTest2.FTItemID = Convert.ToInt32(value[1]);
                    cardaxCsvTest2.PayrollNumber = Convert.ToInt32(value[2]);
                    cardaxCsvTest2.EventDateTime = Convert.ToDateTime(value[3]);
                    cardaxCsvTest2.CardholderFirstName = value[4];
                    cardaxCsvTest2.CardholderLastName = value[5];

                    Globals.CardaxQueryResult.Add(cardaxCsvTest2);
                    }
                }
            }
        }
        catch (Exception)
        {
            myLog.Error("Unable to open/read Cardax simulated punch csv file! " +
                "File already open or does not exist: \"{0}\"", cardaxCsvPath);
        }

Solution

  • EDITED

    If you are lines are not truly blank and contain commas, you can split with RemoveEmptyEntries option and then check the column count.

                    while (!cardaxSR.EndOfStream)
                    {                              // this commented out part is what I would like to work but doesn't seem to work.
                        line = cardaxSR.ReadLine();//.Skip(1).Where(item => !String.IsNullOrWhiteSpace(item));
                        value = line.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);  // <-- Remove empty columns while splitting. It has a side-effect: Any record with just a single blank column will also get discarded by the if that follows.
                        if (value.length < 6)
                          continue;
    
                        CardaxDataObject cardaxCsvTest2 = new CardaxDataObject();
    
                        cardaxCsvTest2.EventID = Convert.ToInt32(value[0]);
                        cardaxCsvTest2.FTItemID = Convert.ToInt32(value[1]);
                        cardaxCsvTest2.PayrollNumber = Convert.ToInt32(value[2]);
                        cardaxCsvTest2.EventDateTime = Convert.ToDateTime(value[3]);
                        cardaxCsvTest2.CardholderFirstName = value[4];
                        cardaxCsvTest2.CardholderLastName = value[5];
    
                        Globals.CardaxQueryResult.Add(cardaxCsvTest2);
                    }
    

    Another improvement feedback I have: When you catch an exception, it's a good practice to log the exception in addition to your custom error line. A custom error line might be good for say website users, but as a developer running some service you will appreciate the actual exception stack trace. It will help you debug a bug easier.

        catch (Exception ex)
        {
            myLog.Error("Unable to open/read Cardax simulated punch csv file! " +
                "File already open or does not exist: \"{0}\".\r\n Exception: {1}", cardaxCsvPath, ex.ToString());
        }