我正在尝试在我的项目中实现FILEHELPER 2.0,但是某些带有“”引用问题的csv文件。它显示错误(显示此错误日志)
**“LineNumber | LineString | ErrorDescription 2 |“符号”,“日期”,“到期”,“行使价”,“开盘价”,“高价”,“低价”,“收盘价”,“LTP”,“结算价”,“合约数量” ,“Lacs的营业额”,“Open Int”,“OI的变化”,“底层价值”|长度不能小于零。 - >参数名称:长度不能小于零。 - >参数名称:长度 3 |” **
我的代码是这样的:
var engine = new FileHelperEngine<script>();
engine.Options.IgnoreFirstLines = 1; // skipping the header line
script[] res = engine.ReadFile("agile.csv"); <<<<< at this line error occred
和我的班级文件:
[DelimitedRecord(",")]
public class script
{
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Symbol;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Date;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Expiry;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
...
}
和csv文件是。
“符号”,“日期”,“到期日”,“行使价”,“开盘价”,“高价”,“低价”,“收盘价”,“LTP”,“结算价”,“合约数量” “,”Lacs的营业额“,”Open Int“,”OI的变化“,”潜在价值“
“NIFTY”,“2012年8月31日”,“2012年9月27日”,“5400.00”,“56.00”,“56.90”,“38.05”,“44.45”,“43.55”,“44.45” ,“281087”,“765592.77”,“4845150”,“1334150”,“5258.50”
答案 0 :(得分:1)
我看不出您的错误的任何原因:以下代码正常工作:
[DelimitedRecord(",")]
public class Script
{
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Symbol;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Date;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Expiry;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string StrikePrice;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Open;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string High;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Low;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string Close;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string LTP;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string SettlePrice;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string NoOfContracts;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string TurnOverInLacs;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string OpenInt;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string ChangeInOI;
[FieldQuoted('"', QuoteMode.AlwaysQuoted)]
public string UnderlyingValue;
}
class Program
{
static void Main(string[] args)
{
string input = @"""Symbol"",""Date"",""Expiry"",""Strike Price"",""Open"",""High"",""Low"",""Close"",""LTP"",""Settle Price"",""No. of contracts"",""Turnover in Lacs"",""Open Int"",""Change in OI"",""Underlying Value """ + Environment.NewLine +
@"""NIFTY"",""31-Aug-2012"",""27-Sep-2012"","" 5400.00"","" 56.00"","" 56.90"","" 38.05"","" 44.45"","" 43.55"","" 44.45"","" 281087"","" 765592.77"","" 4845150"","" 1334150"","" 5258.50""";
var engine = new FileHelperEngine<Script>();
engine.Options.IgnoreFirstLines = 1; // skipping the header line
Script[] validRecords = engine.ReadString(input);
// Check the third record
Assert.AreEqual("NIFTY", validRecords[0].Symbol);
Assert.AreEqual("31-Aug-2012", validRecords[0].Date);
Assert.AreEqual("27-Sep-2012", validRecords[1].Date);
// etc...
Console.WriteLine("All assertions passed");
Console.Read();
}
}