【问题标题】:Filehelpers CSV parsing. How to use FieldQuoted attribute?Filehelpers CSV 解析。如何使用 FieldQuoted 属性?
【发布时间】:2012-05-24 23:25:18
【问题描述】:

我的 CSV 格式如下:

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

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

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

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

你知道我做错了什么吗?

谢谢

【问题讨论】:

    标签: c# parsing csv filehelpers


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,使用了不同的引擎创建方式。您的示例似乎运行良好...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2017-11-25
    • 2014-05-31
    • 2015-12-07
    • 2021-11-18
    • 2013-10-09
    相关资源
    最近更新 更多