I've been trying to parse CSV file:
NOTE: All directories are updated daily using information from the previous business day.
Company Name,Stock Symbol,DPM,Cycle,Traded at C2,LEAPS 2014,LEAPS 2015,LEAPS 2016,Product Types
(NEW) NEWS CORPORATION CLASS A,NWSA,SIG:MU,1,,,,Y,L,
1ST UNITED BANCORP INC (FL),FUBC,WOLVERINE,3,,,,,
21VIANET GROUP INC.,VNET,CITADEL,3,,,,,
22ND CENTURY GROUP INC.,XXII,CITADEL,1,,,,,
3-D SYSTEMS CORP,DDD,SIG,2,,,,Y,L,W,
3M COMPANY,MMM,SIG:MMM,1,Y,,,Y,L,W,
51JOB INC,JOBS,SIG,1,,,,,
58.COM INC.,WUBA,CITADEL,1,,,,,
8X8 INC-NEW,EGHT,SIG,2,,,,,
A A R CORP,AIR,CITADEL,2,,,,,
A V HOMES INC,AVHI,WOLVERINE,3,,,,,
A.M. CASTLE & CO,CAS,CITADEL,3,,,,,
A10 NETWORKS INC.,ATEN,CITADEL,3,,,,,
AARON''S INC,AAN,CITADEL,2,,,,,
with following code (i used substring to get rid of the NOTE section):
void sciagnijSymbole_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
String dane = e.Result.Substring(91);
using (TextReader sr = new StringReader(dane))
{
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<SymbolMap>();
var listaSymboli = csv.GetRecords<Symbol>().ToList();
}
}
public class Symbol
{
public String nazwa { get; set; }
public String symbol { get; set; }
}
public sealed class SymbolMap : CsvClassMap<Symbol>
{
public SymbolMap()
{
Map(m => m.nazwa);
Map(m => m.symbol);
}
}
It throws a CsvMissingFieldException with message: "Fields 'nazwa' do not exist in the CSV file.".I have no idea what's wrong. I want only the first and second column to write to class so I made a map but it says that the field doesn't exist. Also im getting the CSV file from internet so I don't want to change it.
If your property names do not match the column names in your csv file, then you have to indicate the column name in the Map:
public sealed class SymbolMap : CsvClassMap<Symbol>
{
public SymbolMap()
{
Map(m => m.nazwa).Name("Company Name");
Map(m => m.symbol).Name("Stock Symbol");
}
}