我必须阅读衣衫褴褛的正确文件是否有任何方法可以使用文件助手库
来阅读它们我的代码就像这样
[FixedLengthRecord()]
class File_load
{
[FieldFixedLength(10)]
[FieldTrim(TrimMode.Right)]
public string proj_name;
[FieldFixedLength(30)]
[FieldTrim(TrimMode.Right)]
public string iso;
[FieldFixedLength(50)]
[FieldTrim(TrimMode.Right)]
public string line;
[FieldFixedLength(50)]
[FieldTrim(TrimMode.Right)]
public string pid;
}
如果我没有将FieldFixedLength用于最后一列,则会抛出异常
未处理的异常:FileHelpers.BadUsageException:标记为的记录类 FixedLengthRecord属性必须在每个fi中包含FixedLength属性 场。
答案 0 :(得分:0)
您可以使用FixedMode.AllowLessChars
属性的FixedLengthRecord
参数处理您的方案。
这是一个工作程序(我将字段长度更改为2)。
[FixedLengthRecord(FixedMode.AllowLessChars)]
class File_load
{
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string proj_name;
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string iso;
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string line;
[FieldFixedLength(1000)]
[FieldTrim(TrimMode.Right)]
public string pid;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<File_load>();
var records = engine.ReadString(
"112233444444" + Environment.NewLine +
"1122334"
);
var firstRecord = records[0];
Assert.AreEqual("11", firstRecord.proj_name);
Assert.AreEqual("22", firstRecord.iso);
Assert.AreEqual("33", firstRecord.line);
Assert.AreEqual("444444", firstRecord.pid);
var secondRecord = records[1];
Assert.AreEqual("11", secondRecord.proj_name);
Assert.AreEqual("22", secondRecord.iso);
Assert.AreEqual("33", secondRecord.line);
Assert.AreEqual("4", secondRecord.pid);
Console.Read();
}
}