首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileHelpers导入问题

FileHelpers导入问题
EN

Stack Overflow用户
提问于 2013-10-11 23:00:02
回答 1查看 685关注 0票数 0

下午好,我一直在尝试从txt/csv文件中导入字段,但它一直导致导入过程中出现错误。我知道为什么我就是想不出怎么解决这个问题。我在下面列出了一个字符串文件,它在运行时会导致错误。问题是事故描述被正确引用,但也包含另一组双引号。有没有办法从引用的字符串中去掉引号?

代码语言:javascript
复制
"123456","I heard a "pop" in my shoulder","01/01/1900"

这是FileHelpers类

代码语言:javascript
复制
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

任何帮助都是最好的

EN

回答 1

Stack Overflow用户

发布于 2013-10-13 22:35:37

诀窍是不使用FieldQuoted属性,而是应用自定义FieldConverter来删除引号。

代码语言:javascript
复制
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

并更改任何有问题的字段以使用转换器。例如

代码语言:javascript
复制
<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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19321322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档