Filehelpers CSV parsing. How to use FieldQuoted attribute?
Asked Answered
F

1

8

I have a CSV that looks like:

a,b,c
a,b,c
a,"b,c",d

I'm marking 2nd field in delimited class with attribute:

[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.AllowForRead)]
public String ExchangeRate;

But 3rd line still "b,c" being parsed as 2 separate values.

Do you have any idea what am i doing wrong?

Thank you

Fiertz answered 17/5, 2012 at 11:24 Comment(0)
A
13

I can't see anything wrong with your code. I just checked: the following program works fine:

[DelimitedRecord(",")]
public class MyClass
{
    public string Field1;
    [FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.AllowForRead)]
    public string ExchangeRate;
    public string Field3;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<MyClass>();
        string fileAsString = @"a,b,c" + Environment.NewLine +
                              @"a,b,c" + Environment.NewLine + 
                              @"a,""b,c"",d";
        MyClass[] validRecords = engine.ReadString(fileAsString);

        // Check the ExchangeRate values for rows 0, 1, 2 are as expected
        Assert.AreEqual("b", validRecords[0].ExchangeRate);
        Assert.AreEqual("b", validRecords[1].ExchangeRate);
        Assert.AreEqual("b,c", validRecords[2].ExchangeRate);

        Console.ReadKey();
    }
}
Apocarpous answered 17/5, 2012 at 16:18 Comment(1)
Thanks, have used different way of creating engine. Your example seems to be working fine...Fiertz

© 2022 - 2024 — McMap. All rights reserved.