1

我在解析日期值时遇到问题CsvReader。在我的测试数据中,日期采用以下格式:

01/03/2021

因此,我的CsvReader财产是这样设计的:

[Format("dd/MM/yyyy")]
[Name("Date")]
public DateTime Date { get => HistoryWeek.Week; set => HistoryWeek.Week = value; }

以上工作正常。但后来我让一个 beta 用户试用我的应用程序,他们遇到了一个异常:

<?xml version="1.0" encoding="utf-8"?>
<LogEntries>
  <LogEntry Date="2022-01-16 22:15:13" Severity="Exception" Source="MSAToolsLibrary.Importer.Importer.ImportHistoryFromCLMExplorer" ThreadId="1">
    <Exception Type="CsvHelper.ReaderException" Source="CsvHelper.CsvReader+&lt;GetRecords&gt;d__87`1.MoveNext">
      <Message>An unexpected error occurred.
IReader state:
   ColumnCount: 0
   CurrentIndex: 0
   HeaderRecord:
["Date","Status","Weekly Bible Reading","Song1 #","Song1 Title","Song1 Scripture","Song2 #","Song2 Title","Song2 Scripture","Song3 #","Song3 Title","Song3 Scripture","Meeting?","CO Visit","Cancel Reason","# Classes","Chairman","PrayerOpen","PrayerClose","TreasuresTalk","TreasuresTalk_Theme","TreasuresDigging","BibleReading_A","BibleReading_Study_A","BibleReading_B","BibleReading_Study_B","BibleReading_C","BibleReading_Study_C","BibleReading_Source","Apply1_Description","Apply2_Description","Apply3_Description","Apply4_Description","Apply1_A","Apply1_Asst_A","Apply1_Study_A","Apply2_A","Apply2_Asst_A","Apply2_Study_A","Apply3_A","Apply3_Asst_A","Apply3_Study_A","Apply4_A","Apply4_Asst_A","Apply4_Study_A","Apply1_B","Apply1_Asst_B","Apply1_Study_B","Apply2_B","Apply2_Asst_B","Apply2_Study_B","Apply3_B","Apply3_Asst_B","Apply3_Study_B","Apply4_B","Apply4_Asst_B","Apply4_Study_B","Apply1_C","Apply1_Asst_C","Apply1_Study_C","Apply2_C","Apply2_Asst_C","Apply2_Study_C","Apply3_C","Apply3_Asst_C","Apply3_Study_C","Apply4_C","Apply4_Asst_C","Apply4_Study_C","LivingPart1","LivingPart1_Theme","LivingPart1_Length","LivingPart2","LivingPart2_Theme","LivingPart2_Length","CBS","CBS_Source","CBS_Read","Audience B","Audience C","AuxCounselor B","AuxCounselor C"]
IParser state:
   ByteCount: 0
   CharCount: 1483
   Row: 2
   RawRow: 2
   Count: 82
   RawRecord:
1/03/2021,Unscheduled,NUMBERS 7-8,4,“Jehovah Is My Shepherd”,Psalm 23,54,“This Is the Way”,"Isaiah 30:20, 21",127,The Sort of Person I Should Be,2 Peter 3:11,Y,N,,1,,,,,“Lessons From the Camp of Israel”,,,5,,,,,Nu 7:1-17,Mem. Inv.,RV,RV,RV,,,11,,,6,,,12,,,17,,,,,,,,,,,,,,,,,,,,,,,,,,Organizational Accomplishments,(5 min.),,Local Needs,(10 min.),,"rr chap. 5 ¶17-22, box 5A",,,,,

</Message>
      <Exception Type="System.FormatException" Source="System.DateTimeParse.ParseExactMultiple">
        <Message>String was not recognized as a valid DateTime.</Message>
        <StackTrace>   at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style)
   at CsvHelper.TypeConversion.DateTimeConverter.ConvertFromString(String text, IReaderRow row, MemberMapData memberMapData)
   at lambda_method(Closure )
   at CsvHelper.Expressions.RecordCreator.Create[T]()
   at CsvHelper.CsvReader.&lt;GetRecords&gt;d__87`1.MoveNext()</StackTrace>
      </Exception>
    </Exception>
  </LogEntry>
</LogEntries>

我询问了他的数据并注意到他的日期略有不同:

1/03/2021

所以日期的格式是不同的:

  • dd/MM/yyyy
  • d/MM/yyyy

如何更改我的CsvReader财产以应对这种情况并避免异常?


更新

我注意到关于Multiple date format support #603的类似讨论。我需要弄清楚如何采纳那里的建议。这似乎表明可以满足多种日期格式。问题是,我没有使用类映射。

4

0 回答 0