FileHelpers希望每条记录都以一个新行结束,因此您必须在将引擎传递给引擎之前预先解析输入。这是简单的做虽然 - 是这样的:
var lines = File.ReadAllLines(pathToImportFile);
var sb = new StringBuilder();
var separator = ","; // use a comma as field delimiter
foreach (string line in lines)
{
if (String.IsNullOrEmpty(line))
sb.AppendLine(""); // convert empty lines into line feeds
else
sb.AppendFormat("\"{0}\"{1}", line, separator); // put quotes around the field to avoid problems with nested separators
}
var engine = new FileHelperEngine<MyClass>();
engine.ReadString(sb.ToString());
和你的类将看起来像
[DelimitedRecord(",")]
class MyClass
{
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Title;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string FullName;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Address1;
/// ... etc
}
是否有过空白或缺失行? – NotMe 2014-09-10 13:56:09
你可能正则表达式?与@ChrisLively相同的问题:空白行?它是否总是采用这种格式,唯一的变化就是省略手机和电子邮件? – Brandon 2014-09-10 14:11:46
是的,有空行分隔记录,但没有其他人! – 2014-09-10 15:39:15