认为这很容易,但是在FileHelpEngine中找不到任何方法来删除文本或csv文件中的尾随空行,这会导致ReadFile失败。
答案 0 :(得分:3)
如果已知空行数,例如2,则可以在记录类中使用:
[IgnoreLast(2)]
public class ...
另一种选择是忽略空行但在它们出现的任何地方都被忽略
[IgnoreEmptyLines()]
public class ...
您可以尝试的最后一件事是使用INotifyRead接口忽略代码行,如:
[FixedLengthRecord(FixedMode.AllowVariableLength)]
[IgnoreEmptyLines]
public class OrdersFixed
:INotifyRead
{
[FieldFixedLength(7)]
public int OrderID;
[FieldFixedLength(8)]
public string CustomerID;
[FieldFixedLength(8)]
public DateTime OrderDate;
[FieldFixedLength(11)]
public decimal Freight;
public void BeforeRead(BeforeReadEventArgs e)
{
if (e.RecordLine.StartsWith(" ") ||
e.RecordLine.StartsWith("-"))
e.SkipThisRecord = true;
}
public void AfterRead(AfterReadEventArgs e)
{
// we want to drop all records with no freight
if (Freight == 0)
e.SkipThisRecord = true;
}
}