c# - 如何删除 FileHelper 中尾随的空行

标签 c# filehelpers

本以为很简单,但在 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/

相关文章:

c# - 使用自签名证书从 Winforms 客户端对 SSL 保护的 WCF 服务进行身份验证

c# - 为什么在此示例中需要 "where T : class"?

c# - 使用 FileHelper 库解析具有 n 级层次结构的位置记录文件

c# - .NET 如何从匿名类型的枚举中输出 csv?

c# - ReadStreamAsDT - Filehelpers 和 C# - 如何使用 filehelpers 动态读取 CSV?

c# - 使用 CSExeCOMServer 如何在 VS C++ 中接收事件

C# 等到条件为真

c# - 在 ASP.Net 应用程序中安全地存储(加密)数据

c# - 如何检查 Math.cos(angle) = 0

c# - 导入MySQL时格式化数据表日期列