13

Is there a built-in method in .NET that validates csv files/strings?

I would prefer something like this online csv validator but in C#. I have done some research but all I have found are examples of people writing the code themselves (examples were all written a few years ago and might be outdated).

POC:

bool validCSV = CoolCSV_ValidatorFunction(string csv/filePath);
user3772119
  • 484
  • 3
  • 7
  • 16
  • You could try parsing it and if it succeeds then it was valid. So far as actual syntax validation and cleaning, you will have to develop something more robust as there is no method that I know of which will validate it for you. – user1477388 Jul 15 '14 at 18:26
  • Is there a built in validation for CSV in .NET? No. What kind of validation are you looking for? Just check if the syntax is valid (nr of columns etc) or something domain specific? – HaukurHaf Jul 15 '14 at 18:27
  • @HaukurHaf Something that will more or less also check CSV formatting rules (embedded quotes, commas, etc). – user3772119 Jul 15 '14 at 18:35
  • This guy had a similar question: http://stackoverflow.com/questions/9642055/csv-parsing-options-with-net – Fabian Bigler Jul 15 '14 at 18:38

3 Answers3

18

There is! For some reason it's buried in the VB namespace, but it's part of the .NET Framework, you just need to add a reference to the Microsoft.VisualBasic assembly. The type you're looking for is TextFieldParser.

Here is an example of how to validate your file:

using Microsoft.VisualBasic.FileIO;
...
var path = @"C:\YourFile.csv";
using (var parser = new TextFieldParser(path))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");

    string[] line;
    while (!parser.EndOfData)
    {
        try
        {
            line = parser.ReadFields();
        }
        catch (MalformedLineException ex)
        {
            // log ex.Message
        }
    }
}
Justin R.
  • 23,435
  • 23
  • 108
  • 157
  • This is an awesome finding! I war never aware of these namespaces within .Net Framework might contain such gems! – Alexander Galkin Jul 15 '14 at 18:31
  • 2
    Does this handle all the rules for CSV? (quotes, embedded commas, newlines, etc.) – Joe Enos Jul 15 '14 at 18:33
  • Nice find. Had no idea about this! – HaukurHaf Jul 15 '14 at 18:36
  • @JoeEnos Unfortunately, it does not as fas as I know. And for fun: "," is not the delimeter for german csv, it'S ";" – Christian Sauer Jul 15 '14 at 18:41
  • 3
    @JoeEnos It doesn't hit all of RFC 4180 (the de facto CSV standard), and you need to configure the parser to work with your data (e.g., setting `parser.HasFieldsEnclosedInQuotes = true;`), but except for some really out there corner cases, yes it will handle newlines, embedded commas, etc. – Justin R. Jul 15 '14 at 18:43
  • @ChristianSauer you can specify the delimiter to use. E.g.., `parser.SetDelimiters(";");` to handle the German case. – Justin R. Jul 15 '14 at 18:45
  • How to check a File is valid or not before reading? As this is reading from a .jpg file also.its not going to Catch block. – lokanath das Dec 11 '20 at 07:15
5

The best CSV reader I've found is this one from Lumenworks:

http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Very fast, very full-featured. Recommended.

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
2

That CSV Parser also seems promising (Not built-in, though): http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

Related thread: CSV Parsing Options with .NET

Community
  • 1
  • 1
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70