Filehelpers CSV解析。如何使用FieldQuoted属性?

时间:2012-05-17 11:24:31

标签: c# parsing csv filehelpers

我的CSV看起来像:

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

我在分隔的类中使用属​​性

标记第二个字段
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.AllowForRead)]
public String ExchangeRate;

但是第3行仍然将“b,c”解析为2个单独的值。

你知道我做错了什么吗?

谢谢

1 个答案:

答案 0 :(得分:11)

我看不出你的代码有什么问题。我刚检查过:以下程序运行正常:

[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();
    }
}