我们正在使用FileHelpers根据该行的第一个字符将文本文件解析为多个实体。然后,每个实体都存储在特定的数据库表中。除了解析的字段外,我们还希望存储每个输入字符串的整体。
在将输入行解析为实体的各个字段之前,有没有办法捕获输入行?
答案 0 :(得分:1)
您可以使用事件获取整行,BeforeReadRecord或AfterReadRecord具有包含属性RecordLine的参数
这里是一个示例:https://www.filehelpers.net/example/EventsAndNotification/ReadEvents/
[FixedLengthRecord(FixedMode.AllowVariableLength)]
[IgnoreEmptyLines]
public class OrdersFixed
{
[FieldFixedLength(7)]
public int OrderID;
[FieldFixedLength(8)]
public string CustomerID;
[FieldFixedLength(8)]
public DateTime OrderDate;
[FieldFixedLength(11)]
public decimal Freight;
}
public override void Run()
{
var engine = new FileHelperEngine<OrdersFixed>();
engine.BeforeReadRecord += BeforeEvent;
engine.AfterReadRecord += AfterEvent;
var result = engine.ReadFile("report.inp");
foreach (var value in result)
Console.WriteLine("Customer: {0} Freight: {1}", value.CustomerID, value.Freight);
}
private void BeforeEvent(EngineBase engine, BeforeReadEventArgs<OrdersFixed> e)
{
Console.Write(e.RecordLine)
}
private void AfterEvent(EngineBase engine, AfterReadEventArgs<OrdersFixed> e)
{
Console.Write(e.RecordLine)
}