When you've configured a CsvReader
with a configuration to skip empty lines by using IgnoreBlankLines
property of CsvConfiguration
, how can I go about at getting the count of the skipped lines?
Alternatively, if I don't skip them - how do I track them?
It seems that a blank line is represented by the first field having an empty string as value, and then every other field having null - but this is an implementation detail - i.e, it could be possible that actual data came in this format, or am I wrong?
If your file has a header, you could read the header and then get the row number you are on. The following example skips two lines so the row is 3 when the header is read.
void Main()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
IgnoreBlankLines = true
};
using (var reader = new StringReader("\n\nId,Name\n1,One"))
using (var csv = new CsvReader(reader, config))
{
csv.Read();
csv.ReadHeader();
var firstRow = csv.Context.Parser.Row;
var records = csv.GetRecords<Foo>();
Console.WriteLine(firstRow); // 3
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
Getting the row number without a header would be only slightly more complicated.
void Main()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
IgnoreBlankLines = true,
HasHeaderRecord = false
};
using (var reader = new StringReader("\n\n1,One"))
using (var csv = new CsvReader(reader, config))
{
var isFirstRow = true;
var firstRow = 0;
var records = new List<Foo>();
while (csv.Read())
{
if (isFirstRow)
{
firstRow = csv.Context.Parser.Row;
isFirstRow = false;
}
records.Add(csv.GetRecord<Foo>());
}
Console.WriteLine(firstRow); // 3
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}