8

我有一个 CSV,看起来像:

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

我正在用属性标记分隔类中的第二个字段:

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

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

你知道我在做什么错吗?

谢谢

4

1 回答 1

13

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

[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();
    }
}
于 2012-05-17T16:18:37.773 回答