I am reading CSV file using LumenWorks.Framework.IO.Csv. My requirement is to read CSV file and replace empty value in CSV file with NULL and store it into string, to perform some further operation. But Empty value are not being replaced by null in my string. They are being stored as empty character only.
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using LumenWorks.Framework.IO.Csv;
namespace ConsoleApp5
{
internal class Program
{
static void Main(string[] args)
{
using (StreamReader csvFile = new StreamReader("C:/Users/raj/Document/Test.csv", Encoding.UTF8))
{
using (CsvReader csvReader = new CsvReader((TextReader)csvFile, false, ',', '"', '"', '#', ValueTrimmingOptions.All))
{
csvReader.DefaultParseErrorAction = ParseErrorAction.ThrowException;
csvReader.SupportsMultiline = true;
csvReader.SkipEmptyLines = true;
csvReader.MissingFieldAction = MissingFieldAction.ReplaceByNull;
bool flag = true;
int countNull = 0;
int countLargeSize = 0;
int countCorrect = 0;
while (flag)
{
try
{
flag = csvReader.ReadNextRecord();
if (flag)
{
if (csvReader.FieldCount != 150)
throw new MalformedCsvException("Fields do not match!");
string[] strArray = new string[csvReader.FieldCount];
csvReader.CopyCurrentRecordTo(strArray);
//int = csvReader[;
if (((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(r => r == null)) || ((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(r => r.Length > (int)byte.MaxValue)))
{
if (((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(i => i == null)) || ((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(i => i.Length > (int)byte.MaxValue)))
{
if (((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(i => i == null)))
{
countNull++;
}
else if (((IEnumerable<string>)strArray).Any<string>((System.Func<string, bool>)(i => i.Length > (int)byte.MaxValue)))
{
countLargeSize++;
}
}
}
else
{
countCorrect++;
}
}
else
break;
}
catch (MalformedCsvException ex)
{
}
}
Console.WriteLine(countCorrect +" "+ countNull+" "+countLargeSize);
}
}
}
}
}
New contributor
Rajkumar Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.