I'm trying to serialize an XML from the electronic invoice, but when I got to the id element where it is an Array object I packed myself because I get the error "Object reference not set to an instance of an object." Based on an XML from an NFe I generated the classes to be serialized, using the Visual Studio automatic option, edit / paste special / Paste XML as classes.
And to test I made the following code.
private void button1_Click_1(object sender, EventArgs e)
{
try
{
nfeProc nota = new nfeProc();
nota.versao = 3.10M;
nota.NFe = new nfeProcNFe();
nota.NFe.infNFe = new nfeProcNFeInfNFe();
nota.NFe.infNFe.Id = "NFe13130604501136000136650020000973882010222458";
nota.NFe.infNFe.versao = 3.10M;
nota.NFe.infNFe.ide = new nfeProcNFeInfNFeIde();
nota.NFe.infNFe.ide.cUF = 11;
nota.NFe.infNFe.ide.cNF = 01022245;
nota.NFe.infNFe.ide.natOp = "VENDA DE MERCADORIA";
nota.NFe.infNFe.ide.indPag = 0;
nota.NFe.infNFe.ide.mod = 65;
nota.NFe.infNFe.ide.serie = 1;
nota.NFe.infNFe.ide.nNF = 23354;
nota.NFe.infNFe.ide.dhEmi = DateTime.Now;
nota.NFe.infNFe.ide.tpNF = 1;
nota.NFe.infNFe.ide.idDest = 1;
nota.NFe.infNFe.ide.cMunFG = 1100114;
nota.NFe.infNFe.ide.tpImp = 4;
nota.NFe.infNFe.ide.tpEmis = 1;
nota.NFe.infNFe.ide.cDV = 8;
nota.NFe.infNFe.ide.tpAmb = 2;
nota.NFe.infNFe.ide.finNFe = 1;
nota.NFe.infNFe.ide.indFinal = 1;
nota.NFe.infNFe.ide.indPres = 1;
nota.NFe.infNFe.ide.procEmi = 0;
nota.NFe.infNFe.ide.verProc = "1.0";
nota.NFe.infNFe.emit = new nfeProcNFeInfNFeEmit();
nota.NFe.infNFe.emit.CNPJ = 55555555555555;
nota.NFe.infNFe.emit.xNome = "LOJA FAKE CONFECCOES";
nota.NFe.infNFe.emit.enderEmit = new nfeProcNFeInfNFeEmitEnderEmit();
nota.NFe.infNFe.emit.enderEmit.xLgr = "AVENIDA TESTE I";
nota.NFe.infNFe.emit.enderEmit.nro = 1257;
nota.NFe.infNFe.emit.enderEmit.xCpl = "COMERCIO";
nota.NFe.infNFe.emit.enderEmit.xBairro = "BAIRRO";
nota.NFe.infNFe.emit.enderEmit.cMun = 1100114;
nota.NFe.infNFe.emit.enderEmit.xMun = "JARULANDIA";
nota.NFe.infNFe.emit.enderEmit.UF = "RO";
nota.NFe.infNFe.emit.enderEmit.CEP = 76590000;
nota.NFe.infNFe.emit.enderEmit.cPais = 1058;
nota.NFe.infNFe.emit.enderEmit.xPais = "BRASIL";
nota.NFe.infNFe.emit.enderEmit.fone = 333333333;
nota.NFe.infNFe.emit.IE = 2222222;
nota.NFe.infNFe.emit.CRT = 1;
nota.NFe.infNFe.dest = new nfeProcNFeInfNFeDest();
nota.NFe.infNFe.dest.CPF = 98765432198;
nota.NFe.infNFe.dest.xNome = "";
nota.NFe.infNFe.dest.enderDest = new nfeProcNFeInfNFeDestEnderDest();
nota.NFe.infNFe.dest.enderDest.xLgr = "";
nota.NFe.infNFe.dest.enderDest.nro = 0;
nota.NFe.infNFe.dest.enderDest.xCpl = "";
nota.NFe.infNFe.dest.enderDest.xBairro = "";
nota.NFe.infNFe.dest.enderDest.cMun = 0;
nota.NFe.infNFe.dest.enderDest.xMun = "";
nota.NFe.infNFe.dest.enderDest.UF = "";
nota.NFe.infNFe.dest.enderDest.CEP = 0;
nota.NFe.infNFe.dest.enderDest.cPais = 0;
nota.NFe.infNFe.dest.enderDest.xPais = "";
nota.NFe.infNFe.dest.enderDest.fone = 0;
nota.NFe.infNFe.dest.indIEDest = 0;
/*Aqui o problema ao instanciar a classe ide, tentei fazer a instancia usando a linha que está comentada abaixo e deu o mesmo erro*/
//nota.NFe.infNFe.det = new nfeProcNFeInfNFeDet[1];
nota.NFe.infNFe.det[1]= new nfeProcNFeInfNFeDet();
nota.NFe.infNFe.det[0].nItem = 1;
/*fim do campo com problemas*/
XmlSerializer ser = new XmlSerializer(typeof(nfeProc));
FileStream arquivo = new FileStream("xmls/serializado.xml",FileMode.OpenOrCreate);
ser.Serialize(arquivo,nota);
MessageBox.Show("Xml Gerado");
}
catch (Exception erro)
{
MessageBox.Show(erro.Message,"Erro ao gerar NFe");
}
}
Example NFe XML Link View or Download XML
Only instantiated the class det
and assigns a value to the attribute nItem
.