2013-10-11 52 views
0

下午好,我一直在試圖從txt/csv文件中導入一個字段,並且它在導入時不斷導致錯誤。我知道爲什麼我似乎無法弄清楚如何解決它。 我有一個列在下面列出的字符串,它在運行時會導致錯誤。問題是事故描述被正確引用,但也包含另一組雙引號。我有什麼方法可以從引用的字符串中刪除引號嗎?FileHelpers導入問題

"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 

任何幫助將是巨大的

+0

剝離出來我的代碼對不起 –

+0

你是如何讀取文件?......這是今天上午在這裏... – Plutonix

+0

昏暗的引擎=新FileHelperEngine(的GetType(xxx.yyy)) 昏暗的數據作爲xxx.yyy()= DirectCast(engine.ReadFile(Me.txtFileName.Text),xxx.yyy()) –

回答

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