Csv FileHelpers引用字段是否包含单引号?

Csv FileHelpers引用字段是否包含单引号?,csv,filehelpers,Csv,Filehelpers,如何读取包含单引号的FileHelpers引用字段 下面是我的csv记录 "1","7" Screen","Mobile" 型号: [DelimitedRecord(",")] public class LineModel { [FieldQuoted('"', QuoteMode.OptionalForBoth)] public string Id; [FieldQuoted('"', QuoteMode.OptionalForBoth)] public string D

如何读取包含单引号的FileHelpers引用字段

下面是我的csv记录

"1","7" Screen","Mobile"
型号:

[DelimitedRecord(",")]

public class LineModel 
{

 [FieldQuoted('"', QuoteMode.OptionalForBoth)]

 public string Id;

 [FieldQuoted('"', QuoteMode.OptionalForBoth)]

 public string Details;

 [FieldQuoted('"', QuoteMode.OptionalForBoth)]

 public string Device;

}

获取上述记录时出错:-字段详细信息被引用,但被引用的字符:“不在分隔符之前(您可以使用[FieldTrim]来避免此错误)
QuoteMode
在输入文件中有不明确的引号时工作不太好。相反,您可以删除
[FieldQuoted]
属性,并在自定义转换器中处理引号

[DelimitedRecord(",")]
public class LineModel
{
    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Id;

    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Details;

    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Device;
}

public class MyQuotedFieldConverter : ConverterBase
{
    public override object StringToField(string from)
    {
        // If the field starts and ends with a double quote
        if (from.StartsWith("\"") && from.EndsWith("\""))
        {
            // Remove the first and last character
            return from.Substring(1, from.Length - 1);
        }
        return from;
    }
}
当然,如果你在你的领域内有“,”的话,你就会有麻烦

"1","7, Screen","Mobile"
在这种情况下,您必须通过实现
INotifyRead
接口来预解析记录行以清理输入。类似于:

[DelimitedRecord(",")]
public class LineModel : INotifyRead
{
    //... fields as before

    public void BeforeRead(BeforeReadEventArgs e)
    {
        if (e.RecordLine.Count(x => x == ',') > 3)
        {
            e.RecordLine = DetectAndReplaceEmbeddedDelimiters(e.RecordLine);
        }
    }

    public void AfterRead(AfterReadEventArgs e)
    {                
    }
}

另一种考虑反向的方法:使用自定义转换器向每个字段添加引号并删除/替换嵌入的引用。然后使用<代码> QuoToMod。AlcObjuts< <代码> >

<代码>代码> <代码> >当您在输入文件中有歧义引用时,不能很好地工作。相反,您可以删除<代码> [字段引用]。属性,并在自定义转换器中处理引号

[DelimitedRecord(",")]
public class LineModel
{
    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Id;

    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Details;

    [FieldConverter(typeof(MyQuotedFieldConverter))]
    public string Device;
}

public class MyQuotedFieldConverter : ConverterBase
{
    public override object StringToField(string from)
    {
        // If the field starts and ends with a double quote
        if (from.StartsWith("\"") && from.EndsWith("\""))
        {
            // Remove the first and last character
            return from.Substring(1, from.Length - 1);
        }
        return from;
    }
}
当然,如果你在你的领域内有“,”的话,你就会有麻烦

"1","7, Screen","Mobile"
在这种情况下,您必须通过实现
INotifyRead
接口来预解析记录行以清理输入。类似于:

[DelimitedRecord(",")]
public class LineModel : INotifyRead
{
    //... fields as before

    public void BeforeRead(BeforeReadEventArgs e)
    {
        if (e.RecordLine.Count(x => x == ',') > 3)
        {
            e.RecordLine = DetectAndReplaceEmbeddedDelimiters(e.RecordLine);
        }
    }

    public void AfterRead(AfterReadEventArgs e)
    {                
    }
}

另一种考虑反向的方法:使用自定义转换器向每个字段添加引号并删除/替换嵌入引用。然后使用<代码> QuoToMod。