简体   繁体   English

FileHelpers - 错误转换'“”“ID”“'到类型:'Int32'

[英]FileHelpers - Error Converting '“”“ID”"' to type: 'Int32'

The IntValue1 in the csv file is a number without double quotes but when the system reads the row it adds double quotes at the beginning and also double quotes followed by semicolon at the end of the row to delimit the row. csv文件中的IntValue1是一个没有双引号的数字,但是当系统读取该行时,它会在开头添加双引号,并在行的末尾添加双引号,后跟分号以分隔行。 Because of this, the IntValue1 starts with a double quotes and the system doesn't recognize it as an int... Please help me to fix this error. 因此,IntValue1以双引号开头,系统不会将其识别为int ...请帮我修复此错误。

The model: 该模型:

[DelimitedRecord(",")]
public class MyObject
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    private DateTime _EventDate;
    public DateTime EventDate
    {
        get { return _EventDate; }
        set { _EventDate = value; }
    }
    private string _IPAddress;
    public string IPAddress
    {
        get { return _IPAddress; }
        set { _IPAddress = value; }
    }
}

The code for reading & displaying the data: 用于读取和显示数据的代码:

private static void GetCsvData()
{
   var engine = new FileHelperEngine<MyTypeObj>();

   //The error occurs at this line:
   var result = engine.ReadFile("C:\\CsvFileName.csv");

   //Code to display the Data
}

The CSV file looks like this: CSV文件如下所示:

在此输入图像描述

Content of the row returned by the debugger when the error occurs: 发生错误时调试器返回的行的内容: 在此输入图像描述

Error: 错误: 在此输入图像描述

Problem #1: Your file is not a valid CSV. 问题#1:您的文件不是有效的CSV。 The proper way to fix this problem would be to correct the way the file is being exported as it is not exporting a valid CSV file. 解决此问题的正确方法是更正导出文件的方式,因为它不导出有效的CSV文件。

This is not a valid CSV file: 这不是有效的CSV文件:

"ID,""EventDate"",""IPAddress""";
"1,""2013-01-19"",""11.81.11.00""";
"2,""2012-11-25"",""11.72.41.84""";
"3,""2011-12-27"",""15.80.";"3.36"""
"4,""2014-08-17"",""17.72.";"9.24"""
"5,""2012-01-30"",""90.94.27.108""";
"6,""2013-02-15"",""19.97.27.189""";

The Hack: You can use FileHelpers to import invalid CSV files using the BeforeReadRecord event. Hack:您可以使用FileHelpers使用BeforeReadRecord事件导入无效的CSV文件。 Sometimes you are just not in control of what CSV file you may receive for importing. 有时您无法控制可能导入的CSV文件。

Your Model: 你的型号:

[IgnoreFirst]
[DelimitedRecord(",")]
public class MyTypeObj
{
    [FieldConverter(ConverterKind.Int32)]
    public int ID;

    [FieldQuoted]
    [FieldConverter(ConverterKind.Date, "yyyy-mm-dd")]
    public DateTime EventDate;

    [FieldQuoted]
    public string IPAddress;
}

Your code: 你的代码:

var engine = new FileHelperEngine<MyTypeObj>();

engine.BeforeReadRecord += (@base, args) =>
{
    // Convert this: "1,""2013-01-19"",""11.81.11.00""";
    //      to this: 1,"2013-01-19","11.81.11.00"
    args.RecordLine = args.RecordLine
        .Substring(1, args.RecordLine.Length - 3)
        .Replace("\"\"", "\"");
};

var result = engine.ReadFile(@"C:\CsvFileName.csv");

You will still have problems with these lines (why is there a semi-colon in the middle of the value???): 你仍然会遇到这些行的问题(为什么在值的中间有一个分号???):

"3,""2011-12-27"",""15.80.";"3.36"""
"4,""2014-08-17"",""17.72.";"9.24"""

You could also fix these in BeforeReadRecord , but it would be best to figure out what is going wrong with the export. 你也可以在BeforeReadRecord解决这些问题,但最好弄清楚导出出了什么问题。

tl;dr Fix how the file is exported so you get a VALID CSV. tl; dr修复文件的导出方式,以便获得有效的CSV。 Fixing the import should only be an absolute last resort hack. 修复导入应该只是绝对的最后手段hack。

Looking at the quick start example on the FileHelpers website , their example csv only contains data - not a descriptive first row of data. 查看FileHelpers 网站上的快速入门示例,他们的示例csv仅包含数据 - 而不是描述性的第一行数据。

I guess when the FileHelperEngine reads the first item of the first row, which is "ID", it will try to convert it to an integer, which fails... 我想当FileHelperEngine读取第一行的第一项(即“ID”)时,它会尝试将其转换为整数,但失败...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM