public void GerarXml(NFe NFe, string folderDestino)
{
doc = new XmlDocument();
XmlDeclaration node = doc.CreateXmlDeclaration("1.0", "UTF-8", "");
doc.InsertBefore(node, doc.DocumentElement);
///
/// NFe
///
XmlNode xmlInf = doc.CreateElement("NFe");
//if (NFe.ide.cUF != 29) //Bahia
//{
// XmlAttribute xmlVersion = doc.CreateAttribute("xmlns:xsi");
// xmlVersion.Value = "http://www.w3.org/2001/XMLSchema-instance";
// xmlInf.Attributes.Append(xmlVersion);
//}
XmlAttribute xmlVersion1 = doc.CreateAttribute("xmlns");
xmlVersion1.Value = NFeStrConstants.NAME_SPACE_NFE;// !string.IsNullOrEmpty(Propriedade.nsURI_nfe) ? Propriedade.nsURI_nfe : "http://www.portalfiscal.inf.br/nfe";
xmlInf.Attributes.Append(xmlVersion1);
doc.AppendChild(xmlInf);
string cChave = NFe.ide.cUF.ToString() +
NFe.ide.dEmi.Year.ToString("0000").Substring(2) +
NFe.ide.dEmi.Month.ToString("00"); //data AAMM
if (NFe.infNFe.Versao >= 3)
cChave = NFe.ide.cUF.ToString() +
NFe.ide.dhEmi.Substring(2, 2) +
NFe.ide.dhEmi.Substring(5, 2); //data AAMM
Int64 iTmp = Convert.ToInt64("0" + NFe.emit.CNPJ + NFe.emit.CPF);
cChave += iTmp.ToString("00000000000000");
cChave += Convert.ToInt32(NFe.ide.mod).ToString("00");
if (NFe.ide.cNF == 0)
{
///
/// gera codigo aleatorio
///
NFe.ide.cNF = this.GerarCodigoNumerico(NFe.ide.nNF);
}
string ccChave = cChave +
NFe.ide.serie.ToString("000") +
NFe.ide.nNF.ToString("000000000") +
((int)NFe.ide.tpEmis).ToString("0") +
NFe.ide.cNF.ToString("00000000");
if (NFe.ide.cDV == 0)
{
///
/// calcula digito verificador
///
NFe.ide.cDV = this.GerarDigito(ccChave);
}
else
{
int ccDV = this.GerarDigito(ccChave);
if (NFe.ide.cDV != ccDV)
throw new Exception(string.Format("Digito verificador informado, [{0}] é diferente do calculado, [{1}]", NFe.ide.cDV, ccDV));
}
cChave += NFe.ide.serie.ToString("000") +
NFe.ide.nNF.ToString("000000000") +
((int)NFe.ide.tpEmis).ToString("0") +
NFe.ide.cNF.ToString("00000000") +
NFe.ide.cDV.ToString("0");
NFe.infNFe.ID = cChave;
///
/// infNFe
///
if (NFe.infNFe.Versao == 0) NFe.infNFe.Versao = Convert.ToDecimal(versoes.VersaoXMLNFe.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator));
nDecimaisPerc = (double)NFe.infNFe.Versao >= 3.10 ? TpcnTipoCampo.tcDec4 : TpcnTipoCampo.tcDec2;
XmlElement infNfe = doc.CreateElement("infNFe");
XmlAttribute infNfeAttr1 = doc.CreateAttribute(TpcnResources.versao.ToString());
infNfeAttr1.Value = NFe.infNFe.Versao.ToString("0.00").Replace(",", ".");
infNfe.Attributes.Append(infNfeAttr1);
XmlAttribute infNfeAttrId = doc.CreateAttribute(TpcnResources.Id.ToString());
infNfeAttrId.Value = "NFe" + NFe.infNFe.ID;
infNfe.Attributes.Append(infNfeAttrId);
xmlInf.AppendChild(infNfe);
this.convertToOem = true;
infNfe.AppendChild(GerarIde(NFe));
infNfe.AppendChild(GerarEmit(NFe));
GerarAvulsa(NFe, infNfe);
XmlNode nodeDest = GerarDest(NFe);
if (nodeDest != null && nodeDest.HasChildNodes) ///danasa 6/2014
infNfe.AppendChild(nodeDest); ///somente grava o nó "dest" se exibir
GerarRetirada(NFe, infNfe);
GerarEntrega(NFe, infNfe);
GerarautXML(NFe, infNfe);
GerarDet(NFe, infNfe);
GerarTotal(NFe, infNfe);
GerarTransp(NFe.Transp, infNfe);
GerarCobr(NFe.Cobr, infNfe);
if (NFe.infNFe.Versao >= 3 && NFe.ide.mod == TpcnMod.modNFCe)
GerarPag(NFe, infNfe);
GerarInfAdic(NFe.InfAdic, infNfe);
GerarExporta(NFe, NFe.exporta, infNfe);
GerarCompra(NFe.compra, infNfe);
GerarCana(NFe.cana, infNfe);
this.cFileName = NFe.infNFe.ID + Propriedade.ExtEnvio.Nfe;
if (!string.IsNullOrEmpty(folderDestino))
{
if (folderDestino.Substring(folderDestino.Length - 1, 1) == @"\")
folderDestino = folderDestino.Substring(0, folderDestino.Length - 1);
if (!Directory.Exists(Path.Combine(folderDestino, "Convertidos")))
{
///
/// cria uma pasta temporária para armazenar o XML convertido
///
System.IO.Directory.CreateDirectory(Path.Combine(folderDestino, "Convertidos"));
}
string strRetorno = this.cFileName;
strRetorno = Path.Combine(folderDestino, "Convertidos");
this.cFileName = Path.Combine(strRetorno, this.cFileName);
///
/// salva o XML
strRetorno = this.cFileName;
doc.Save(@strRetorno);
}
///
/// retorna o conteudo do XML da nfe
XMLString = doc.OuterXml;
}