在FileHelpers C#中的字段内忽略NewLIne

时间:2017-07-25 10:05:52

标签: c# csv filehelpers

我使用FileHelpers Class解析CSV是相当新的。我有这样的文件...

    "background","Info","Agent abcdefg
    ===================
    Context Tenant: {Vendor: 1, Customer: 719046046}","2,140.69","","7/11/2017 3:30 AM"

我想忽略任何换行符,并希望将其作为一个字符串读取。你能帮忙吗?

所以在该行的末尾是\ r \ n,我希望将其删除。

2 个答案:

答案 0 :(得分:2)

您可以像这样使用[FieldQuoted]

public class YourRecordClass
{
  [FieldQuoted()]
  public string Field1;

  [FieldQuoted()]
  public string Field2;

  [FieldQuoted(QuoteMode.OptionalForBoth, MultiLineMode.AllowForBoth)]
  public string Field3;

  [FieldQuoted()]
  public string Field4;

  [FieldQuoted()]
  public string Field5;

  [FieldQuoted()]
  public string Field6;
}

答案 1 :(得分:1)

[FieldQuoted(QuoteMode.OptionalForBoth, MultiLineMode.AllowForBoth)][FieldQuoted()]不适用于我的情况。

我最终要做的是使用File.ReadAllText读取文件,将\r\n替换为空值,然后将读取的字符串提供给FileHelperEngine``ReadString

在我的情况下,csv是用\n行结尾生成的,而人工注释是使用\r\n添加的

  var engine = new FileHelperEngine<LLU>();

  var fileText = File.ReadAllText("myfile.csv");
  fileText = Regex.Replace(fileText, @"\r\n", ""); // Remove lines added by humans in comments

  var result = engine.ReadString(fileText)