FileIO.TextFieldParser 获取未更改的行以报告失败的解析
FileIO.TextFieldParser get unaltered row for reporting on failed parse
如果出现错误我想输出当前行,但我收到一条消息说当前记录什么都没有。
这是我的代码:
Dim currentRow As String()
Using MyReader As New FileIO.TextFieldParser(filenametoimport)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{currentrow}]")
report.AppendLine("- record is malformed and will be skipped. ")
Continue While
End Try
End While
end Using
我需要输出当前行,以便我可以向用户报告有错误记录。
report.AppendLine($"[{currentrow}]")
我知道如果解析失败该值将为 null 但有没有办法获取当前记录?
解析记录失败,如何输出这条记录?
感谢您的协助!
我在 MyReader.SetDelimiters(",")
上没有遇到编译错误,但我还是将其更改为数组。 report.AppendLine($"[{currentrow}]")
行可能不需要数组。我更改了该行以提供一个字符串。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentRow As String() = Nothing
Using MyReader As New FileIO.TextFieldParser("filenametoimport")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters({","})
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{String.Join(",", currentRow)}]")
report.AppendLine("- record is malformed and will be skipped. ")
Continue While
End Try
End While
End Using
End Sub
编辑
根据@Joel Coehoorn 和@ErocM 的评论,如果该行为空,您可以提供上一行的内容,以便找到错误的行。
Dim LastGoodRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
LastGoodRow = currentRow
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{String.Join(",", LastGoodRow)}]")
report.AppendLine("- record following this row is malformed and will be skipped. ")
Continue While
End Try
End While
异常中无法直接获取原始数据,但至少可以获取到错误发生的行号。您可以使用该行号返回并找到有问题的记录:
Dim currentRow As String()
Using MyReader As New FileIO.TextFieldParser(filenametoimport)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine($"{vbCrLf}- record at line {ex.LineNumber} is malformed and will be skipped. ")
End Try
End While
End Using
TextFieldParser
还提供了对底层流的访问,并提供了一个 ReadLine()
方法,因此如果您真的不想编写代码,您可以将流返回到上一行结尾然后调用 MyReader.ReadLine()
获取记录(这会反过来将流再次推进到您期望的位置)。
如果出现错误我想输出当前行,但我收到一条消息说当前记录什么都没有。
这是我的代码:
Dim currentRow As String()
Using MyReader As New FileIO.TextFieldParser(filenametoimport)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{currentrow}]")
report.AppendLine("- record is malformed and will be skipped. ")
Continue While
End Try
End While
end Using
我需要输出当前行,以便我可以向用户报告有错误记录。
report.AppendLine($"[{currentrow}]")
我知道如果解析失败该值将为 null 但有没有办法获取当前记录?
解析记录失败,如何输出这条记录?
感谢您的协助!
我在 MyReader.SetDelimiters(",")
上没有遇到编译错误,但我还是将其更改为数组。 report.AppendLine($"[{currentrow}]")
行可能不需要数组。我更改了该行以提供一个字符串。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim currentRow As String() = Nothing
Using MyReader As New FileIO.TextFieldParser("filenametoimport")
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters({","})
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{String.Join(",", currentRow)}]")
report.AppendLine("- record is malformed and will be skipped. ")
Continue While
End Try
End While
End Using
End Sub
编辑
根据@Joel Coehoorn 和@ErocM 的评论,如果该行为空,您可以提供上一行的内容,以便找到错误的行。
Dim LastGoodRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
LastGoodRow = currentRow
Catch ex As FileIO.MalformedLineException
report.AppendLine()
report.AppendLine($"[{String.Join(",", LastGoodRow)}]")
report.AppendLine("- record following this row is malformed and will be skipped. ")
Continue While
End Try
End While
异常中无法直接获取原始数据,但至少可以获取到错误发生的行号。您可以使用该行号返回并找到有问题的记录:
Dim currentRow As String()
Using MyReader As New FileIO.TextFieldParser(filenametoimport)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
ImportLine(currentRow)
Catch ex As FileIO.MalformedLineException
report.AppendLine($"{vbCrLf}- record at line {ex.LineNumber} is malformed and will be skipped. ")
End Try
End While
End Using
TextFieldParser
还提供了对底层流的访问,并提供了一个 ReadLine()
方法,因此如果您真的不想编写代码,您可以将流返回到上一行结尾然后调用 MyReader.ReadLine()
获取记录(这会反过来将流再次推进到您期望的位置)。