简体   繁体   中英

Why should I mark the last column of class model as optional when importing a CSV file with FileHelpers?

I read many questions here on StackOverflow trying to find a solution to a simple problem. I'm importing a CSV file using FileHelpers library. The CSV file has no delimiter at the last column, and when I try to import I get the error

Line: 2 Column: 179. Delimiter ',' not found after field 'Active' (the record has less fields, the delimiter is wrong or the next field must be marked as optional)

It's right, because my file looks like

...,Clip Ons,,D02,8 Card Wallet,Y
...,D02,Bathurst Chain Crossbody,Y

One solution I found is to mark the last column using attribule FieldOptional . The problem is that the column isn't optional; it must throw an error if the last column is null.

How can I handle this situation avoiding ' FieldOptional ' attribute?

Which version are you using? With FileHelpers 3.1.5, this works fine:

[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class MySpec
{
    public string Column1;
    public string Column2;
    public string Column3;
    public string Column4;
    public string Column5;
}

class Program
{
    static void Main(string[] args)
    {
        var fileHelpersEngine = new FileHelperEngine<MySpec>();
        var records = fileHelpersEngine.ReadString("Clip Ons,,D02,8 Card Wallet,Y");
        var firstRecord = records.First();
        Assert.AreEqual("Clip Ons", firstRecord.Column1);
        Assert.AreEqual(string.Empty, firstRecord.Column2);
        Assert.AreEqual("D02", firstRecord.Column3);
        Assert.AreEqual("8 Card Wallet", firstRecord.Column4);
        Assert.AreEqual("Y", firstRecord.Column5);
        Console.ReadKey();
    }
}

With older versions (2.0 if I remember correctly), you needed to add one more extra (dummy) property and mark it [FieldOptional] . This means: the last delimiter is optional and I don't care about any contents after the last delimiter .

So your class would then look like this:

[DelimitedRecord(",")]
[IgnoreEmptyLines]
public class MySpec
{
    public string Column1;
    public string Column2;
    public string Column3;
    public string Column4;
    public string Column5;
    [FieldOptional]
    public string Dummy;
}

This class also works with the above example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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