Header with name [0] not found

Viewed 3297

I have this csv file i need to be able to read and I cant get it to work. here is the csv and a picture of csv. Im not sure what the problem is, I have been following the tutorials so I have a guess that it might be something with the csv file and the spacing it is using. I cannot change anything in the csv. I have tried adding Name properties to the class and also index properties.

CsvHelper version: 27.1.0

public static void Main(string[] args)
    {

        using (var streamReader = new StreamReader(@"C:\Users\Adam\Desktop\C#\price_detail.csv"))
        {
            using (var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture))
            {
                var records = csvReader.GetRecords<SKU>();
            }
        }
    }




public class SKU
{
    //[Name("PriceValueId")]
    public string PriceValueId { get; set; }

    //[Name("Created")]
    public DateTime Created { get; set; }

    //[Name("Modified")]
    public DateTime Modified { get; set; }

    //[Name("CatalogEntryCode")]
    public string CatalogEntryCode { get; set; }

    //[Name("MarketId")]
    public string MarketId { get; set; }

    //[Name("CurrencyCode")]
    public string CurrencyCode { get; set; }

    //[Name("ValidFrom")]
    public DateTime ValidFrom { get; set; }

    //[Name("ValidUntil")]
    public DateTime ValidUntil { get; set; }

    //[Name("UnitPrice")]
    public decimal UnitPrice { get; set; }

}

Debug output:

Header with name 'PriceValueId'[0] was not found.

Header with name 'Created'[0] was not found.

Header with name 'Modified'[0] was not found.

Header with name 'CatalogEntryCode'[0] was not found.

Header with name 'MarketId'[0] was not found.

Header with name 'CurrencyCode'[0] was not found.

Header with name 'ValidFrom'[0] was not found.

Header with name 'ValidUntil'[0] was not found.

Header with name 'UnitPrice'[0] was not found.

If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.

IReader state:

   ColumnCount: 0

   CurrentIndex: -1

   HeaderRecord:

["PriceValueId  Created Modified    CatalogEntryCode    MarketId    CurrencyCode    ValidFrom   ValidUntil  UnitPrice"]

IParser state:

   ByteCount: 0

   CharCount: 101

   Row: 1

   RawRow: 1

   Count: 1

   RawRecord:

PriceValueId    Created Modified    CatalogEntryCode    MarketId    CurrencyCode    ValidFrom   ValidUntil  UnitPrice
1 Answers

I think you already figured out you need to change the delimiter to be a tab. You also need to make sure the property ValidUntil is nullable and then add "NULL" as a NullValue to the TypeConverterOptionsCache

public static void Main(string[] args)
{
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = "\t"
    };

    using (var streamReader = new StreamReader(@"C:\Users\Adam\Desktop\C#\price_detail.csv"))
    using (var csvReader = new CsvReader(streamReader, config))
    {
        csvReader.Context.TypeConverterOptionsCache.GetOptions<DateTime?>().NullValues.Add("NULL");
        
        var records = csvReader.GetRecords<SKU>();
    }   
}

public class SKU
{
    public string PriceValueId { get; set; }
    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public string CatalogEntryCode { get; set; }
    public string MarketId { get; set; }
    public string CurrencyCode { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime? ValidUntil { get; set; }
    public decimal UnitPrice { get; set; }
}
Related