GERAR O ARQUIVO XML PARA MATRIZ DE TRIBUTAÇÃO DO IMPORTADOR
Autor: I love Helle Berry. | Publicado: 2014-02-28T05:35:00-08:00


&nbsp;&nbsp; 
Olá, Pessoal!

O código abaixo faz a geração de dados para a MATRIZ DE TRIBUTAÇÃO DO IMPORTADOR, novo serviço disponível no site da Sefaz de alguns estados brasileiros.

Este código-fonte está em VISUAL STUDIO 2012 para VB.NET e já dá uma idéia de como gerar o XML para atender aos requesitos da Sefaz.


&nbsp;Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Try

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Catch ex As Exception

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;End Try
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim Ano As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim Mes As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim Dia As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sDataIni As String = Now
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim myConnectionMatrix As MySqlConnection
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim myCommand2IM As MySqlCommand
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim dsR As MySqlDataReader

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Ano = Trim(Replace(Mid(sDataIni, 7, 4), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Mes = Trim(Replace(Mid(sDataIni, 4, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dia = Trim(Replace(Mid(sDataIni, 1, 2), "/", ""))


&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sInicio = Ano + "-" + Mes + "-" + Dia


&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sTimes = CStr(Now)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sHora = Mid(sTimes, 12, 8)

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sDataGeracao As String = Ano + "-" + Mes + "-" + Dia + " " + sHora

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myConnectionMatrix = New MySqlConnection("server=192.168.40.65; user id=root; password=3007722108; database=orca; pooling=false;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myConnectionMatrix.Open()

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim myCommandsEQ As MySqlCommand
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sSequencia As String = ""

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sSQL = "SELECT ifnull(count(sequencial),0) FROM GERARXML"
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myCommandsEQ = New MySqlCommand(sSQL, myConnectionMatrix)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR = myCommandsEQ.ExecuteReader

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;If dsR.Read() Then

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sSequencia = dsR(0)

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;End If

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR.Close()

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sSequencia = sSequencia + 1

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'If Len(sSequencia) = 1 Then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;' sNumeroMatrizDi = "0000000000000" + sSequencia
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;' ElseIf Len(sSequencia) = 2 Then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'sNumeroMatrizDi = "000000000000" + sSequencia
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'ElseIf Len(sSequencia) = 3 Then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'sNumeroMatrizDi = "00000000000" + sSequencia
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'ElseIf Len(sSequencia) = 4 Then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'sNumeroMatrizDi = "0000000000" + sSequencia
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'End If

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Gerar sequencial da XML
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sSQL = "INSERT INTO GERARXML VALUES(" &amp; CInt(sSequencia) &amp; ",'" &amp; sDataGeracao &amp; "','" &amp; sNumeroMatrizDi &amp; "')"
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Pega os valores das data definidas dentro da tabela padrão
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myCommand2IM = New MySqlCommand(sSQL, myConnectionMatrix)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR = myCommand2IM.ExecuteReader

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR.Close()


&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Ler os dados&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sSQL = "SELECT * FROM matriztrib WHERE DATAREGISTRO &nbsp;= '" &amp; sInicio &amp; "' ORDER BY CODPROD"

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Pega os valores das data definidas dentro da tabela padrão
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myCommand2IM = New MySqlCommand(sSQL, myConnectionMatrix)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR = myCommand2IM.ExecuteReader

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sInicioBenf As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sFimBenef As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sImportador As String = ""
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Gerar o XML de cada item
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Abre o Arquivo para Criação do XML
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'----------------------------------
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'sNumeroMatrizDi = sNumeroMatrizDi + 1

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'Cabeçalho do Arquivo
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'--------------------
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim sData As String = CStr(Now)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Ano = Trim(Replace(Mid(sData, 7, 4), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Mes = Trim(Replace(Mid(sData, 4, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dia = Trim(Replace(Mid(sData, 1, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sArquivo = "MatrizDI_" + sNumeroMatrizDi + "_" + DateTime.Now.ToString("ddMMyy") + "_" + DateTime.Now.ToString("hhmmss") + ".xml"
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim arq As New StreamWriter("C:\Temp\" + sArquivo, False)

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'arq.WriteLine("ï»¿" &amp; "&lt;?xml version=" &amp; Chr(34) &amp; "1.0" &amp; Chr(34) &amp; " encoding=" &amp; Chr(34) &amp; "UTF-8" &amp; Chr(34) &amp; "??&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;?xml version=" &amp; Chr(34) &amp; "1.0" &amp; Chr(34) &amp; " encoding=" &amp; Chr(34) &amp; "UTF-8" &amp; Chr(34) &amp; "??&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;enviMatrizDIe xmlns=" &amp; Chr(34) &amp; "http://www.sefaz.am.gov.br/die" &amp; Chr(34) &amp; " xmlns:xsi=" &amp; Chr(34) &amp; "http://www.w3.org/2001/XMLSchema-instance" &amp; Chr(34) &amp; " versao=" &amp; Chr(34) &amp; "1.00" &amp; Chr(34) &amp; " xsi:schemaLocation=" &amp; Chr(34) &amp; "http://www.sefaz.am.gov.br/die enviGabaritoDIe_v1.00.xsd" &amp; Chr(34) &amp; "?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;MatrizDIe?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;InfMatrizDIe Id=" &amp; Chr(34) &amp; "MatrizDIe" &amp; sNumeroMatrizDi &amp; Chr(34) &amp; " versao=" &amp; Chr(34) &amp; "1.00" &amp; Chr(34) &amp; "?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;Importador?&gt;")



&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;If dsR.Read Then
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sImportador = dsR(0)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sImportador = Replace(sImportador, ".", "")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sImportador = Replace(sImportador, "-", "")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;tipoImportador?&gt;" &amp; dsR(1) &amp; "&lt;/tipoImportador?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdImportador?&gt;" &amp; sImportador &amp; "&lt;/cdImportador?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;End If

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR.Close()
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;/Importador?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;myCommand2IM = New MySqlCommand(sSQL, myConnectionMatrix)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR = myCommand2IM.ExecuteReader



&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;While dsR.Read

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;ItemMatriz?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdNcmProdFinal?&gt;" &amp; dsR(2) &amp; "&lt;/cdNcmProdFinal?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdSuframa?&gt;" &amp; dsR(3) &amp; "&lt;/cdSuframa?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdDestinacao?&gt;" &amp; dsR(4) &amp; "&lt;/cdDestinacao?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdUtilizacao?&gt;" &amp; dsR(5) &amp; "&lt;/cdUtilizacao?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;cdTributacao?&gt;" &amp; dsR(6) &amp; "&lt;/cdTributacao?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;numDecreto?&gt;" &amp; Replace(dsR(7), "/", "") &amp; "&lt;/numDecreto?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;tpDocumentoConcessivo?&gt;" &amp; "1" &amp; "&lt;/tpDocumentoConcessivo?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;numDocumentoConcessivo?&gt;" &amp; Replace(dsR(8), "/", "") &amp; "&lt;/numDocumentoConcessivo?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sInicioBenf = dsR(9)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ano = Trim(Replace(Mid(sInicioBenf, 7, 4), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mes = Trim(Replace(Mid(sInicioBenf, 4, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dia = Trim(Replace(Mid(sInicioBenf, 1, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sInicioBenf = Ano + Mes + Dia

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;dtInicioBeneficio?&gt;" &amp; sInicioBenf &amp; "&lt;/dtInicioBeneficio?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sFimBenef = dsR(10)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ano = Trim(Replace(Mid(sFimBenef, 7, 4), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Mes = Trim(Replace(Mid(sFimBenef, 4, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dia = Trim(Replace(Mid(sFimBenef, 1, 2), "/", ""))
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sFimBenef = Ano + Mes + Dia

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;dtFimBeneficio?&gt;" &amp; sFimBenef &amp; "&lt;/dtFimBeneficio?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;/ItemMatriz?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;End While

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;/InfMatrizDIe?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;/MatrizDIe?&gt;")
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.WriteLine("&lt;/enviMatrizDIe?&gt;")

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;dsR.Close()
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;arq.Close()

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;' Create an XML document instance and load the XML data.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Dim doc As XmlDocument = New XmlDocument()
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;' This code assumes that the XML file is in the same folder.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;doc.Load("C:\Temp\" + sArquivo)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;' Save the XML to a file.
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;doc.Save("C:\Temp\" + sArquivo)

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MsgBox("Arquivo XML criado com sucesso", MsgBoxStyle.Information, "Matriz Trubutação")


&nbsp;&nbsp; &nbsp;End Sub

Pontosa observar:

1. A notação "?&gt;" ou&nbsp;"??&gt;"&nbsp;&nbsp;no final de cada tag deve ser substituída por "&gt;" ou "/&gt;" conforme o caso. Trata-se do editor de textos do blog que infelizmente foge ao meu controle!

2. A notação "&amp;" é o "é comercial" necessário para separar campos no VB.NET;
3.&nbsp;o campo Id (MatrizDIe) é&nbsp;a identificação da matriz, e o número que o segue deve,&nbsp;
para facilitar, referir-se à inscrição da empresa no Cadastro&nbsp;de Contribuintes do Estado com 14 casas, ou seja,&nbsp;a informação é numérica e o comprimento da informação é&nbsp;
de 14 dígitos. Alguns estão colocando aqui um contador, mas este não é o procedimento
correto.&nbsp;

4. Aqui transformamos o arquivo-texto criado em um documento XML propriamente dito:




&nbsp;&nbsp;' Create an XML document instance and load the XML data.

&nbsp;&nbsp;Dim doc As XmlDocument = New XmlDocument()

&nbsp;&nbsp;' This code assumes that the XML file is in the same folder.

&nbsp;&nbsp;doc.Load("C:\Temp\" + sArquivo)



No momento em que doc.Load é realizado o arquivo XML é verificado em relação ao arquivo XSD &nbsp; carregado inicialmente - linha que contêm o&nbsp;http://www.w3.org/2001/XMLSchema-instance.&nbsp;



Você pode acrescentar aqui um comando Try para indicar os erros dos arquivos XML que está gerando.





&nbsp;&nbsp;' Save the XML to a file.

&nbsp;&nbsp; doc.Save("C:\Temp\" + sArquivo)



Após o salvamento o arquivo XML está pronto para ser assinado. A assinatura pode ser realizada com um certificado digital do Serasa e vocês podem utilizar o mesmo código-fonte que postei aqui para realizar a assinatura deste documento.

Segue um bom site que fala um pouco sobre assinaturas digitais:

http://inginheiiro.wikidot.com/



Sucesso a todos em 2014!