本以为很简单,但在 FileHelpEngine 中找不到任何方法来删除文本或 csv 文件中的尾随空行,这会导致 ReadFile 失败。
最佳答案
如果空行数已知,例如 2,您可以在记录类中使用:
[IgnoreLast(2)]
public class ...
另一个选项是忽略空行,但在它们出现的任何地方都会被忽略
[IgnoreEmptyLines()]
public class ...
您可以尝试的最后一件事是使用 INotifyRead 接口(interface)通过代码忽略某些行,例如:
[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;
}
}
关于c# - 如何删除 FileHelper 中尾随的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200490/