Importing CSV files into a .NET object isn’t too tricky and for this I usually use CsvHelper. However, sometimes I need to import a CSV file and only extract a couple of columns from it and these columns aren’t always guaranteed to exist. In order to do this I use the following code.

string filePath = "C:/Temp/test.csv"

// Check if file exists.
if (File.Exists(filePath))
{

        CsvConfiguration csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            HasHeaderRecord = true,
            Delimiter = ","
        };

	// Read column headers from file
	CsvReader csv = new CsvReader(File.OpenText(filePath), csvConfiguration);
	csv.Read();
	csv.ReadHeader();

	List<string> headers = csv.HeaderRecord.ToList();

	// Read csv into datatable
	System.Data.DataTable dataTable = new System.Data.DataTable();

	foreach (string header in headers)
	{
		dataTable.Columns.Add(new System.Data.DataColumn(header));
	}

	// Check all required columns are present
	if (!headers.Exists(x => x == "ua_device_type"))
	{
		throw new ArgumentException("ua_device_type field not present in input file.");
	}
	else if (!headers.Exists(x => x == "ua_channel"))
	{
		throw new ArgumentException("ua_channel field not present in input file.");
	}
	else if (!headers.Exists(x => x == "message_type"))
	{
		throw new ArgumentException("message_type field not present in input file.");
	}

	// Import csv
	while (csv.Read())
	{
		System.Data.DataRow row = dataTable.NewRow();

		foreach (System.Data.DataColumn column in dataTable.Columns)
		{
			row[column.ColumnName] = csv.GetField(column.DataType, column.ColumnName);
		}

		dataTable.Rows.Add(row);
	}
	Log.Logger.Debug("File loaded into datatable");

	// Make sure all required columns are populated
	if (dataTable.Select("ua_channel = '' OR ua_channel = null").Count() > 0)
	{
		throw new ArgumentException("Output contains null ua_channel values.");
	}

	if (dataTable.Select("ua_device_type = '' OR ua_device_type = null").Count() > 0)
	{
		throw new ArgumentException("Output contains null ua_device_type values.");
	}

	if (dataTable.Select("message_type = '' OR message_type = null OR message_type not in ('push', 'message centre')").Count() > 0)
	{
		throw new ArgumentException("Output contains null or invalid message_type values.");
	}
}

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *