0

Good afternoon, i've been trying to import a field from a txt/csv file and it keeps causing an error in my import. I know why i just can't seem to figure out how to resolve it. I have a string filed listed below that is causing an error when it runs. The issue is that the accident description is quoted correctly but also contains another set of double quotes. Is there any way for me to strip out quotes from the quoted string ?

"123456","I heard a "pop" in my shoulder","01/01/1900"

This is the FileHelpers class

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

any help would be great

shamp00
  • 11,106
  • 4
  • 38
  • 81

1 Answers1

0

The trick is to NOT use the FieldQuoted attribute, but instead apply a custom FieldConverter to remove the quotes.

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

And change any problem fields to use the converter. For example

<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
shamp00
  • 11,106
  • 4
  • 38
  • 81