c# – FileHelpers在字段中引用和逗号
发布时间:2020-12-15 22:43:27 所属栏目:百科 来源:网络整理
导读:我有一个csv文件,我正在使用FileHelpers解析,我有一个情况,引号和逗号都可以出现在字段中: 逗号: 323,"PC","28/02/2014","UNI001","5000","Return","Returned Goods,damaged",88.00,15.40,"T1","N",0.00,"R","-", 引用 148,"SI","13/01/2014","CGS001","40
我有一个csv文件,我正在使用FileHelpers解析,我有一个情况,引号和逗号都可以出现在字段中:
逗号: 323,"PC","28/02/2014","UNI001","5000","Return","Returned Goods,damaged",88.00,15.40,"T1","N",0.00,"R","-", 引用 148,"SI","13/01/2014","CGS001","4000",1,"5","17" Monitor",266.00,45.39,"Y",311.39, 我的班级是: [DelimitedRecord(",")] public class Transaction { public int TRAN_NUMBER; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string TypeText; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string DATE; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string TransactionAccount; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string NOMINAL_CODE; public int DEPT_NUMBER; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string INV_REF; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string DETAILS; public string NET_AMOUNT; public string TAX_AMOUNT; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string TaxCodeName; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string PAID_FLAG; public string AMOUNT_PAID; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string VatReconText; [FieldQuoted('"',QuoteMode.OptionalForBoth)] public string BankReconText; public string RECON_DATE; } 我找到了这个帖子 engine.BeforeReadRecord += (sender,args) => args.RecordLine = args.RecordLine.Replace(@"""","'"); 但它只能帮助引号出现问题,而不是逗号. 是否可以使用FileHelper解决这两个问题,或者我应该寻找替代解决方案? 解决方法
您可以实现BeforeReadRecord事件来“修复”坏线.
FileHelperEngine engine = new FileHelperEngine<Transaction>(); engine.BeforeReadRecord += BeforeEvent; private void BeforeEvent(EngineBase engine,BeforeReadRecordEventArgs e) { var line = e.RecordLine; // you have to write the following replacement routine... var fixedLine = ReplaceEmbeddedCommasAndQuotesWithSomethingDifferent(line); e.RecordLine = fixedLine; // replace the line with the fixed version } 在您阅读完记录后,您可以处理它们以反转更换过程以修复它们. 如果您更喜欢在FileHelpers类中定义所有逻辑,则可以实现INotifyRead< Transaction>而不是使用事件. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |