Filehelpers - 在每个字段周围用双引号导出EXCEPT当字段为空/空时

时间:2016-06-01 22:25:02

标签: c# filehelpers

我可能在搜索时错过了答案但是我有一个文件需要所有字段双引号除了字段为空,缺少或为null之后只输入逗号。

我正在使用[FieldQuoted('"', QuoteMode.AlwaysQuoted)]以及输出后的示例:

"Mary","Smith","555-555-5555","","1234","","3141 Pi Cr."

但我需要输出实际看起来像这样:

"Mary","Smith","555-555-5555",,"1234",,"3141 Pi Cr."

使用Filehelpers的任何建议?

1 个答案:

答案 0 :(得分:1)

您可以在写入文件之前使用INotifyWrite事件修改输出。

例如

[DelimitedRecord(",")]
class Product : INotifyWrite // <-- implement events
{
    [FieldQuoted(QuoteMode.AlwaysQuoted)]
    public string Name;
    [FieldQuoted(QuoteMode.AlwaysQuoted)]
    public string Description;
    [FieldQuoted(QuoteMode.AlwaysQuoted)]
    public string Size;

    public void BeforeWrite(BeforeWriteEventArgs e)
    {
    }

    public void AfterWrite(AfterWriteEventArgs e)
    {
        // replace any occurrences of ,"", with ,,
        e.RecordLine = e.RecordLine.Replace(",\"\",", ",,");
    }
}