Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:37:33+00:00 2026-06-13T12:37:33+00:00

I have some really lame Csv files I need to parse. I am using

  • 0

I have some really lame Csv files I need to parse. I am using CsvHelper and it is working awesome. Except I have some lines that have whitespace where normaly I have a double.

File:

Text,SomeDouble,MoreText

“Good”,1.23,”Good”

“Bad”, ,”Bad”

if I try and map this into

public class Test
{
  [CsvField(Name = "Text")]
  public string Text { get; set; }

  [CsvField(Name = "SomeDouble")]
  public double? SomeDouble{ get; set; }

  [CsvField(Name = "MoreText")]
  public string MoreText{ get; set; }
}

then I get an error like this:

CsvHelper.CsvReaderException: An error occurred trying to read a
record of type

Row: ‘2’ (1-based)

Field Index: ‘1’ (0-based)

Field Name: ‘SomeDouble’

Field Value: ‘ ‘

System.Exception: is not a valid value for Double. —>
System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options,
NumberFormatInfo numfmt) at
System.ComponentModel.DoubleConverter.FromString(String value,
NumberFormatInfo formatInfo) at
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value) — End of inner
exception stack trace — at
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value) at
System.ComponentModel.NullableConverter.ConvertFrom(ITypeDescriptorContext
context, CultureInfo culture, Object value) at
lambda_method(Closure , ICsvReader ) at
CsvHelper.CsvReader.d__0`1.MoveNext()

As I see it, my options are to create a custom parser, or map my value into a string property and do the parsing there.

Are there any other options?

It would be nice if I could configure that I want to treat white space as null.

As requested, here is a code sample that reproduces the problem

 static class Program
    {
        public class Test
        {
            [CsvField(Name = "Text")]
            public string Text { get; set; }

            [CsvField(Name = "SomeDouble")]
            public double? SomeDouble { get; set; }

            [CsvField(Name = "MoreText")]
            public string MoreText { get; set; }
        }

        static void Main(string[] args)
        {
            // create fake in memory file
            var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream);
            streamWriter.WriteLine("Text,SomeDouble,MoreText");
            streamWriter.WriteLine("Good, 1.23, Good");
            streamWriter.WriteLine("Bad, ,Bad");

            streamWriter.Flush();

            //reset the file to the begining
            memoryStream.Position = 0;

            using (
                var csv =
                    new CsvReader(
                        new StreamReader(memoryStream)))
            {
                // this call will blow up with the exception.
                var records = csv.GetRecords<Test>().ToList();

                //carry on and do stuff with 'records'...
            }
    }

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:37:34+00:00Added an answer on June 13, 2026 at 12:37 pm

    In the end I went with creating my own type converter that will treat whitespace the same as a null.

    public class WhiteSpaceToNullableTypeConverter<T> : TypeConverter where T : struct
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof (string);
        }
    
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof (T?);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                           object value)
        {
            T? result = null;
    
            var stringValue = (string) value;
            if (!string.IsNullOrWhiteSpace(stringValue))
            {
                var converter = TypeDescriptor.GetConverter(typeof(T));
                result = (T)converter.ConvertFrom(stringValue.Trim());
            }
    
            return result;
        }
    
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
                                         object value, Type destinationType)
        {
            var result = (T?) value;
            return result.ToString();
        }
    }
    

    Apply it to your model like this

    public class Test
    {
        [CsvField(Name = "Text")]
        public string Text { get; set; }
    
        [CsvField(Name = "SomeDouble")]
        [TypeConverter( typeof( WhiteSpaceToNullableTypeConverter<Double> ) )]
        public double? SomeDouble{ get; set; }
    
        [CsvField(Name = "MoreText")]
        public string MoreText{ get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some really simple json I need to parse and then run conditional
I have some really complicated legacy code I've been working on that crashes when
We have some really old code that calls WebServices using behaviours (webservice.htc), and we
I have some really simple code that's just not working: int[] manualPixels = new
I have some really nice Python code to do what I need to do.
I have some ideas, some that I have accumulated over time, but I really
I have done some research and it really seems that implementing a transaction system
I really need some help with this as I have been trying to fix
I have been using TortoiseSVN for some time and I really like it. I
I don't see why this doesn't work. I assume that for some really lame

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.