0

下午好,我一直在尝试从 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

任何帮助都会很棒

4

1 回答 1

0

诀窍是不使用该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 DateTime
于 2013-10-13T14:35:36.827 回答