1

I am Using LumenWorks CsvReader for processing the CSV file. Currently i am doing validations for the CSV file where i need to throw error if the Header is empty for a field.

Example = Below is CSV file contents

Header 1,Header 2,Header 3 ,Header 4,Header 5,
Die 11,Die 22,Die 33,Die 44,Die 55,Test
Die 111,Die 222,Die 333,Die 444,Die 555,

Here "Test" is the extra field where Header does not exits. But i observed that CsvReader is adding a default header field called "Column5", instead of throwing error.

Here is the code snippet for reading and processing the CSV File.

        //Read the CSV FIle
        using (CsvReader csv = new CsvReader(new StreamReader(path), true))
        {
           //Set the missing Field Action.
            csv.MissingFieldAction = MissingFieldAction.ParseError;

            //Set Parse Action Error.
            csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
            csv.SkipEmptyLines = true;             


            int fieldCount = csv.FieldCount;
            string[] headers = csv.GetFieldHeaders();


            //Do the processing of the Data.
            while (csv.ReadNextRecord())
            {
                XElement row = new XElement("KS");
                for (int i = 0; i < fieldCount; i++)
                {
                    XElement rowValue = new XElement("K");
                    XAttribute HeaderName = new XAttribute("N", 
                    headers[i]);
                    XAttribute HeaderValue = new XAttribute("V", csv[i]);
                    rowValue.Add(HeaderName);
                    rowValue.Add(HeaderValue);
                    row.Add(rowValue);
                }
                root.Add(row);
            }
         }
    }  

I was expecting that an error should be thrown if there are fields with no headers, but it is adding "Column5". Is there any way where we can throw the error in LumenWorks CSV reader.

The following are the references which i tried to get

http://www.nudoq.org/#!/Packages/LumenWorks.Framework.IO/LumenWorks.Framework.IO/CsvReader

Thanks, Akshay

Akshay Vijapur
  • 310
  • 4
  • 16
  • Did you try to see if HeaderName is null or has value? And if so take necessary actions? – vsarunov May 03 '19 at 05:33
  • Hi, my csv file is dynamic, i dont know how many headers will be there, So i need to go through each field and check weather it has proper header or "Column". – Akshay Vijapur May 03 '19 at 06:07
  • So you need to introduce some validation to check for the scenarios you want to throw the exception, otherwise I do not see how you would achieve this if it handles the cases you want to throw the exception. I would suggest actually not throwing any exception and stay away from them. – vsarunov May 03 '19 at 06:46

0 Answers0