簡體   English   中英

在 C# 中使用 FileHelpers 讀取包含不同長度的分隔字段的分隔記錄?

[英]Read delimited record containing delimited field with varying length with FileHelpers in C#?

我正在使用 MultiRecordEngine() 讀取沒有 header 和不同類型記錄的 delimited.txt 文件。 任何記錄的前兩個字符是我用來識別記錄類型的標識符。

我遇到的問題是在 RecordB 中(參見下面的示例 model),我有一個字段包含多個值,這些值由文件中使用的相同分隔符分隔。 而且字段長度也不是固定的,它的長度取決於其中值的數量。 例如FieldB-0000-1111-2222-3333-FieldD 0000-1111-2222-3333FieldC中讀取 0000-1111-2222-3333 。

注意: FieldB包含一個 integer,表示將出現在FieldC中的值的數量

我正在使用 MultiRecordEngine() 讀取文件和 map 記錄到我的模型以獲取不同類型的記錄。

一個看起來接近我所擁有的樣本:

1 個樣品 model

[DelimitedRecord("-")]
public class RecordB
{
    public string RecordIdentifier { get; set; }
    public string FieldA { get; set; }
    public string FieldB { get; set; } <-- contains a value determining the amount of values in FieldC
    public string FieldC { get; set; } <-- read delimited values to this field [delimiter: -]
    public string FieldD { get; set; }
    public string FieldE { get; set; }
}

使用 MultiRecordEngine 讀取文件

new MultiRecordEngine(RecordSelector,
    typeof(RecordA),
    typeof(RecordB),
    typeof(RecordC),
);

我的 RecordSelector 看起來類似於:

switch (recordType)
{
    case "XX":
        return typeof(RecordA);
    case "XY":
        return typeof(RecordB);
    default:
        return null;
}

我確實嘗試在 model 上使用[DelimitedRecord("-")][FixedLengthRecord(FixedMode.AllowLessChars)]的組合以及在字段上使用[FieldDelimiter("~")]但沒有運氣。 有什么我想念的嗎? 請指教。

我找到了解決方法。 我使用下面的 model 只讀取我知道的包含分隔字段的記錄,稍后拆分值並按索引獲取它們。

[FixedLengthRecord(FixedMode.ExactLength)]
public class RecordB
{
    [FieldFixedLength(100)]
    public string WholeRecord { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM