3

I am fairly new using FileHelpers Class for parsing the CSV. I have a file like this...

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

I would like to ignore any newline and would like to read that as one string. Can you please help?

so at the end of the line it is \r\n and i want this to be removed.

Marcos Meli
  • 3,468
  • 24
  • 29
Mahesh
  • 35
  • 7
  • 1
    File.ReadAllText? – Derek Jul 25 '17 at 10:09
  • I am sorry Derek, that didn't work, or may be i am not sure how to implement. Can you provide some code please – Mahesh Jul 25 '17 at 10:45
  • The FileHelpers library should support new lines, in fields, if the fields are properly quoted (also see [this](https://stackoverflow.com/questions/11511192/handling-newline-in-delimitedrecord-with-filehelpers)). It looks like your input fulfills this requirement. What exactly does not work? Additionally, maybe do you just need a `.Replace(Environment.NewLine, "")` on the field value you read? – Christian.K Jul 25 '17 at 11:25
  • i tried using the Replace method as suggested, however it still appends the empty values to the each property in the object. – Mahesh Jul 25 '17 at 11:38

2 Answers2

3

You can use [FieldQuoted] like this:

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;
}
Marcos Meli
  • 3,468
  • 24
  • 29
  • [FieldQuoted(QuoteMode.OptionalForBoth, MultiLineMode.AllowForBoth)] this does not change anything for newline as far as my example has shown. thanks – MindRoasterMir Jan 20 '19 at 16:58
1

[FieldQuoted(QuoteMode.OptionalForBoth, MultiLineMode.AllowForBoth)] and [FieldQuoted()] didn't work for my case.

What I end up doing is reading the file using File.ReadAllText, replacing \r\n with an empty value and then feeding the read string to FileHelperEngine``ReadString.

In my case the csv was generated with \n line endings and human comments were added using \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)
afarazit
  • 4,907
  • 2
  • 27
  • 51