XML Nfe

Você precisa estar logado para dar um feedback. Clique aqui para efetuar o login
Para efetuar o download você precisa estar logado. Clique aqui para efetuar o login
Confirmar voto
0
 (1)  (1)

Neste Artigo vamos escrever a Nfe em c# 1/1

Neste artigo mostro como escrever os nós da Nfe em c# pela já conhecida Classe XmlTextWriter está incompleto mas já da para ser ter uma ideia e finalizar, nos próximos artigos iremos editar o xml e consumir os webservices da NFE um abraço!!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
 public void Salva_ArquivoXml() {
   try
   {   UTF8Encoding utf = new UTF8Encoding();
       XmlTextWriter textWriter = new XmlTextWriter("C:\\myXmFile.xml",utf);
       textWriter.WriteStartDocument();
       textWriter.WriteStartElement("Nfe","http://www.portalfiscal.inf.br/nfe");// Nó com tipo de Overload
       textWriter.WriteStartElement("infNFe");//Startando o elemento

        textWriter.WriteStartAttribute("Id");//Atributos do Nó
        textWriter.WriteString("Nfe" + "35090448124770000172550010000000020000000820");// escrevendo no atributo
        textWriter.WriteEndAttribute();// finalizando o atributo

        textWriter.WriteStartAttribute("versao");//Atributos do Nó
          textWriter.WriteString("1.10");
        textWriter.WriteEndAttribute();


         textWriter.WriteStartElement("ide");//ide
       
              
          textWriter.WriteStartElement("cUF");//Tag codigo Uf
          textWriter.WriteString("000001");// Código da Uf
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("cNF");//Tag codigo Uf
          textWriter.WriteString("000000027");// Código da Nf
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("natOp");//Tag codigo Uf
          textWriter.WriteString("VENDAS DE PRODUCAO DO ESTABELECIMENTO");// Natureza da Operação
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("indPag");//Tag codigo Uf
          textWriter.WriteString("1");// indicacao de pagamento
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("serie");//Tag codigo Uf
          textWriter.WriteString("1");// Série da Nota
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("nNF");//Tag codigo Uf
          textWriter.WriteString("11");// Numero da NF
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("dEmi");//Tag codigo Uf
          textWriter.WriteString("2009-05-18");// Data da Emissão
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("dSaiEnt");//Tag codigo Uf
          textWriter.WriteString("2009-05-18");// Data da Saida
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("tpNF");//Tag codigo Uf
          textWriter.WriteString("1");// Tipo da Nf
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("cMunFG");//Tag codigo Uf
          textWriter.WriteString("5220280");// Código do municipio fator Gerador
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("tpImp");//Tag codigo Uf
          textWriter.WriteString("1");// Tipo de impressao
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("tpEmis");//Tag codigo Uf
          textWriter.WriteString("1");// Tipo de Emissao
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("cDV");//Tag codigo Uf
          textWriter.WriteString("4");// Código verificador
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("tpAmb");//Tag codigo Uf
          textWriter.WriteString("2");// Tipo de ambiente
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("finNFe");//Tag codigo Uf
          textWriter.WriteString("1");// Finalidade da Nfe
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("procEmi");//Tag codigo Uf
          textWriter.WriteString("1");// Proc Nfe
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("verProc");//Tag codigo Uf
          textWriter.WriteString("1");// Versao da Proc Nfe
          textWriter.WriteEndElement(); 
        textWriter.WriteEndElement();//ide

        textWriter.WriteStartElement("emit");//ide

          textWriter.WriteStartElement("CNPJ");//Tag codigo Uf
          textWriter.WriteString("04140021000163");// CNPJ Emitente
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("xNome");//Tag codigo Uf
          textWriter.WriteString("ACCESS");// Versao da Proc Nfe
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("xFant");//Tag codigo Uf
          textWriter.WriteString("ACCESS");// Versao da Proc Nfe
          textWriter.WriteEndElement();

          textWriter.WriteStartElement("enderEmit");//Tag codigo Uf

            textWriter.WriteStartElement("xLgr");//Tag codigo Uf
            textWriter.WriteString("RUA JOAO DE CARVALHO");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("nro");//Tag codigo Uf
            textWriter.WriteString("15");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("xBairro");//Tag codigo Uf
            textWriter.WriteString("SE");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("cMun");//Tag codigo Uf
            textWriter.WriteString("5220280");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("xMun");//Tag codigo Uf
            textWriter.WriteString("SAO PATRICIO");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("UF");//Tag codigo Uf
            textWriter.WriteString("SP");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("CEP");//Tag codigo Uf
            textWriter.WriteString("SP");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("cPais");//Tag codigo Uf
            textWriter.WriteString("1058");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("xPais");//Tag codigo Uf
            textWriter.WriteString("Brasil");// Versao da Proc Nfe
            textWriter.WriteEndElement();

            textWriter.WriteStartElement("fone");//Tag codigo Uf
            textWriter.WriteString("33414646");// Versao da Proc Nfe
            textWriter.WriteEndElement();
            
        textWriter.WriteEndElement();//enderEmit

          textWriter.WriteStartElement("IE");//Tag codigo Uf
          textWriter.WriteString("2254545");// Versao da Proc Nfe
          textWriter.WriteEndElement();

        textWriter.WriteEndElement();//emit
     textWriter.WriteStartElement("dest");//dest

        textWriter.WriteStartElement("CNPJ");//Tag codigo Uf
        textWriter.WriteString("13955471000103");// Versao da Proc Nfe
        textWriter.WriteEndElement();

        textWriter.WriteStartElement("xNome");//Tag codigo Uf
        textWriter.WriteString("JOSE FIRMINO e CIA LTDA");// Versao da Proc Nfe
        textWriter.WriteEndElement();

        textWriter.WriteStartElement("enderDest");//enderDest

           textWriter.WriteStartElement("xLgr");//Tag codigo Uf
           textWriter.WriteString("RUA JOAO DE CARVALHO");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("nro");//Tag codigo Uf
           textWriter.WriteString("10");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("xBairro");//Tag codigo Uf
           textWriter.WriteString("SE");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("cMun");//Tag codigo Uf
           textWriter.WriteString("3500303");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("xMun");//Tag codigo Uf
           textWriter.WriteString("AGUAI");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("UF");//Tag codigo Uf
           textWriter.WriteString("SP");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("CEP");//Tag codigo Uf
           textWriter.WriteString("01512030");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("cPais");//Tag codigo Uf
           textWriter.WriteString("1058");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("xPais");//Tag codigo Uf
           textWriter.WriteString("Brasil");// Versao da Proc Nfe
           textWriter.WriteEndElement();

           textWriter.WriteStartElement("fone");//Tag codigo Uf
           textWriter.WriteString("33414646");// Versao da Proc Nfe
           textWriter.WriteEndElement();
 
        textWriter.WriteEndElement();//enderDest

          textWriter.WriteStartElement("IE");//Tag codigo Uf
          textWriter.WriteString("03542907");// Versao da Proc Nfe
          textWriter.WriteEndElement();  

     textWriter.WriteEndElement();//dest

      textWriter.WriteStartElement("entrega");//Tag codigo Uf

         textWriter.WriteStartElement("CNPJ");//Tag codigo Uf
         textWriter.WriteString("13955471000103");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("xLgr");//Tag codigo Uf
         textWriter.WriteString("RUA JOAO DE CARVALHO");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("nro");//Tag codigo Uf
         textWriter.WriteString("10");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("xBairro");//Tag codigo Uf
         textWriter.WriteString("SE");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("cMun");//Tag codigo Uf
         textWriter.WriteString("3500303");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("xMun");//Tag codigo Uf
         textWriter.WriteString("Aguai");// Versao da Proc Nfe
         textWriter.WriteEndElement();

         textWriter.WriteStartElement("UF");//Tag codigo Uf
         textWriter.WriteString("SP");// Versao da Proc Nfe
         textWriter.WriteEndElement();     

       textWriter.WriteEndElement();//entrega 
       int incrementoProd = 1; 
       for (int i = 1; i <= 2; i++)//Itens da Nota
       {
           textWriter.WriteStartElement("det");//Tag codigo Uf
              textWriter.WriteStartAttribute("nItem");//Atributos do Nó
                  textWriter.WriteString(incrementoProd.ToString());
                    textWriter.WriteEndAttribute();// finalizando o atributo
                      textWriter.WriteStartElement("prod");//Tag codigo Uf

                          textWriter.WriteStartElement("cProd");//Tag codigo Uf
                          textWriter.WriteString("2");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("cEAN");//Tag codigo Uf
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("xProd");//Tag codigo Uf
                          textWriter.WriteString("TELHA COMERCIAL ROMANA VIDRO");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("NCM");//Tag codigo Uf
                          textWriter.WriteString("10111212");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("CFOP");//Tag codigo Uf
                          textWriter.WriteString("5101");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("uCom");//Tag codigo Uf
                          textWriter.WriteString("CX");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("qCom");//Tag codigo Uf
                          textWriter.WriteString("200.0000");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("vProd");//Tag codigo Uf
                          textWriter.WriteString("863.90");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("cEANTrib");//Tag codigo Uf
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("uTrib");//Tag codigo Uf
                          textWriter.WriteString("CX");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("qTrib");//Tag codigo Uf
                          textWriter.WriteString("200.0000");// Versao da Proc Nfe
                          textWriter.WriteEndElement();

                          textWriter.WriteStartElement("vUnTrib");//Tag codigo Uf
                          textWriter.WriteString("4.3195");// Versao da Proc Nfe
                          textWriter.WriteEndElement();  
    
                            
      
                            
                      
                   textWriter.WriteEndElement();     
             textWriter.WriteEndElement();     
       incrementoProd++ ;
       }



           textWriter.WriteEndElement();//InfNFE
textWriter.WriteEndElement();//Nfe
        
textWriter.Close();
MessageBox.Show("Arquivo exportado");
  }
          catch(Exception e) {

              MessageBox.Show("Houve algum problema");  
          
          }
            

              
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Salva_ArquivoXml();
        }
    }
}

 
Você precisa estar logado para dar um feedback. Clique aqui para efetuar o login
Receba nossas novidades
Ficou com alguma dúvida?