下午好,我一直在尝试从txt/csv文件中导入字段,但它一直导致导入过程中出现错误。我知道为什么我就是想不出怎么解决这个问题。我在下面列出了一个字符串文件,它在运行时会导致错误。问题是事故描述被正确引用,但也包含另一组双引号。有没有办法从引用的字符串中去掉引号?
"123456","I heard a "pop" in my shoulder","01/01/1900"这是FileHelpers类
Namespace xxx
<DelimitedRecord(","), IgnoreFirst(1)>
Public Class yyy
<FieldQuoted()> _
Public IDAs String
<FieldQuoted()> _
Public AccidentDescr As String
<FieldQuoted()> <FieldConverter(ConverterKind.Date, "yyyy-MM-dd")> _
Public DOI As DateTime任何帮助都是最好的
发布于 2013-10-13 22:35:37
诀窍是不使用FieldQuoted属性,而是应用自定义FieldConverter来删除引号。
Public Class MyQuotedStringConverter
Inherits ConverterBase
Public Overrides Function StringToField(from As String) As Object
' StringToField() is used by import. The 'from' parameter will contain all the text between the comma delimiters (including all quotes)
Dim result As String = from
' remove the first and last quotes
If result IsNot Nothing Then
If result.StartsWith("""") Then
result = result.SubString(1)
End If
If result.EndsWith("""") Then
result = result.SubString(0, result.Length - 1)
End If
End If
Return result
End Function
Public Overrides Function FieldToString(fieldValue As Object) As String
' FieldToString is used by export
Return fieldValue.ToString()
End Function
End Class并更改任何有问题的字段以使用转换器。例如
<DelimitedRecord(","), IgnoreFirst(1)>
Public Class yyy
<FieldConverter(GetType(MyQuotedStringConverter))> _
Public IDAs String
<FieldConverter(GetType(MyQuotedStringConverter))> _
Public AccidentDescr As String
<FieldQuoted()> <FieldConverter(ConverterKind.Date, "yyyy-MM-dd")> _
Public DOI As DateTimehttps://stackoverflow.com/questions/19321322
复制相似问题