3

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
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Adam Lundberg
  • 51
  • 1
  • 3

4 Answers4

3

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; }
}
David Specht
  • 7,784
  • 1
  • 22
  • 30
1

In my case, I add csvReader.ReadHeader() and it works.

using (var streamReader = new StreamReader(CSV_FILE_PATH))
{
    using (var csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture))
    {
        csvReader.Read();
        csvReader.ReadHeader();
        var records = csvReader.GetRecords<SKU>();
    }
}

Hope this helps someone.

Hieu
  • 268
  • 6
  • 9
0

CsvReader accepts comma as default delimiter, so if your file has a different delimiter like semicolon, you need to define CsvReader like this.

using (var streamReader = new StreamReader(FILE_PATH))
using (var csvReader = new CsvReader(streamReader, new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ";", Encoding = Encoding.UTF8 }))
{
    var records = csvReader.GetRecords<SKU>().ToList();                  
}
MrAlbino
  • 308
  • 2
  • 10
0

I had the same problem and my csv was a very regular, comma-separated file. I then compared it in Notepad++ against another csv that reads fine. The problem was that the bad file has its encoding in Unix style, while the good one had it in Windows style.

enter image description here

Michael Bahig
  • 748
  • 8
  • 17