Alexandre
Alexandre

Reputation: 2025

Linq error when read invalid chars

I wrote this code to represent the error I'm having, good, when I go to the file input, it falls in the try / catch it just prints on the screen, the line is the invalid character ... good, I know that the error is invalid character, but I need to use this format for encoding have accents in our language, and I need to process them correctly .... but when a character is invalid, it's error in linq.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var lines = File.ReadAllLines(args[0], Encoding.GetEncoding(1252));

                var sortedLines = lines.Skip(1)
                          .Where(x => !Regex.IsMatch("[\r\n]+", x))
                          .Take(lines.Length - 1)
                          .ToArray();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

File Input sample:

014082013  
1BR 116 S\N
9      

The invalid character is: \

So, how I can solve this ?

Upvotes: 0

Views: 202

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66389

The error is coming from the Regular Expression, not LINQ.

You passed the line as the pattern, so "\N" is invalid escape sequence. Note that it's case sensitive so if you will have such line:

1BR 116 S\n

You won't get error with your existing code.

Anyway, to solve this just switch the order of arguments:

.Where(x => !Regex.IsMatch(x, "[\r\n]+"))

As a rule of thumb, never show only the exception message when having more than one line in the try..catch block, always show the whole exception including stack trace so you can debug those errors properly instead of guessing the origin of the error.

Upvotes: 4

Related Questions