4
I have a TXT file where I need to validate the number of characters of each column according to the layout of each document.
Note: Documents are all in one file *.txt
In the second column is the document type "0000,0400,0430". Each document has a different layout that has to be checked.
Layout documento 0000
Coluna 1 = 6 caracteres obrigatórios
Coluna 2 = 4 caracteres obrigatórios
Coluna 3 = 8 caracteres obrigatórios
Coluna 4 <= 100 caracteres maximo
Coluna 5 = 1 caracteres obrigatórios
Coluna 6 = 7 caracteres obrigatórios
Coluna 11 = 1 caractere ou vazio
000001|0000|00000000|ABCDFG|U|1234567|201601|201601|2|1||1|123456|1.0|1
000002|0400|123456|1|123456|2|ABCDFG|123456|1234567|1||
000003|0430|123456|1010101111|111000001|0,00|0,00|0,00|0,00||0,00|5,00|0,00|||
I have that code
class Program
{
    static void Main(string[] args)
    {
        string caminhoArquivo = "C:\\Temp\\Teste.txt";
        try
        {
              var consulta =
              from linha in File.ReadAllLines(caminhoArquivo)
              let clienteDados = linha.Split('|')
              where clienteDados[1] == "0000"
              select new Cliente()
              {
                  Num_Linha = clienteDados[0],
                  Reg = clienteDados[1],
                  CNPJ = clienteDados[2],
                  Nome = clienteDados[3],
              };
            foreach (var item in consulta)
            {               
                Console.WriteLine(item.Num_Linha + "|" + item.Reg + "|" + item.CNPJ + "|" + item.Nome + "|");
                Console.WriteLine(item.Num_Linha.Length == 6);
                Console.WriteLine(item.Reg.Length == 4);
                Console.WriteLine(item.CNPJ.Length == 8);
                Console.WriteLine(item.Nome.Length <= 100);
                Console.WriteLine(item.Reg);
                Console.ReadKey();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(" Erro : " + ex.Message);
        }
    }
}
Customer.class
public class Cliente
  {       
    public string CNPJ { get; set; }
    public string Nome { get; set; }
    public string Num_Linha { get; set; }
    public string Reg { get; set; }
}
You just want to validate or use the file?
– Randrade
Just do the validation.
– Britto
@Britto, this file seems to be from SPED, am I right? If yes I have a code that can help you.
– Robss70
Yes @Robss70 I need a help on this
– Britto
Do you need to validate all the records or just a few? Because if you are going to validate all before importing you will have to have a class with all kinds of records and this is an absurd job. Before importing the file, it is usually only valid the record |0000| and the total count of record lines |9999|. After imported you only treat the registration that interests you. But I will post an example and see if it helps you.
– Robss70