CsvMissingFieldException

CsvMissingFieldException

我一直在尝试解析CSV文件:

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,,,,,


使用以下代码(我使用子字符串摆脱了NOTE部分):

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);
    }
}


它抛出一条CsvMissingFieldException并显示消息:“ CSV文件中不存在字段'nazwa'。”。我不知道这是怎么回事。我只想将第一列和第二列写给类,所以我做了一张地图,但它说该字段不存在。我也从互联网上获取了CSV文件,所以我不想更改它。

最佳答案

如果属性名称与csv文件中的列名称不匹配,则必须在Map中指定列名称:

public sealed class SymbolMap : CsvClassMap<Symbol>
{
    public SymbolMap()
    {
        Map(m => m.nazwa).Name("Company Name");
        Map(m => m.symbol).Name("Stock Symbol");
    }
}

关于c# - CsvHelper引发CsvMissingFieldException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24355174/

10-13 03:12