コード例 #1
0
ファイル: belGerarxml.cs プロジェクト: dramosti/GeraXml_2.0
        /// <summary>
        /// Gera a Estrutura do XML da NF-e
        /// </summary>
        /// <param name="sNF"></param>
        /// <param name="cert"></param>
        /// <param name="sEmp"></param>
        /// <param name="sNomeArq"></param>
        /// <param name="iStatusAtualSistema"</param>
        public void geraArquivoNFE(List<string> sNF, X509Certificate2 cert, string sEmp, string sNomeArq)
        {
            objbelGeraXml = new belGerarXML();
            Conn = objbelGeraXml.Conn;
            Conn.Open();
            try
            {
                string sPath = "";
                sPath = (sFormaEmiNFe.Equals("2") ? CarregarDadosXml("PastaContingencia").ToString() + @sNomeArq : CarregarDadosXml("PastaXmlEnvio").ToString() + @sNomeArq);
                Globais glob = new Globais();
                int iCount = 0;

                if (File.Exists(sPath))
                {
                    File.Delete(sPath);
                }


                foreach (var i in lTotNota)
                {
                    string sNota = sNF[iCount];
                    string sNFe = "NFe" + GeraChave(sEmp, sNota, Conn);
                    XDocument xdoc = new XDocument();

                    #region XML_Principal
                    XNamespace pf = "http://www.portalfiscal.inf.br/nfe";
                    XContainer conenv = new XElement(pf + "enviNFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"),
                                                               new XAttribute("versao", "2.00"),
                                                               new XElement(pf + "idLote", sNomeArq.Substring(7, 15)));
                    #endregion

                    #region XML_Cabeçalho


                    XContainer concabec = (new XElement(pf + "NFe", new XAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe")));
                    XContainer coninfnfe = (new XElement(pf + "infNFe", new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XAttribute("Id", sNFe),
                                                                        new XAttribute("versao", "2.00")));


                    #endregion

                    #region Ide
                    XContainer conide;
                    try
                    {
                        belIde objide = i[0] as belIde;
                        #region XML_ide
                        conide = (new XElement(pf + "ide", new XElement(pf + "cUF", objide.Cuf.ToString()),
                                                                    new XElement(pf + "cNF", objide.Cnf.ToString()),
                                                                    new XElement(pf + "natOp", objide.Natop.ToString()),
                                                                    new XElement(pf + "indPag", objide.Indpag.ToString()),
                                                                    new XElement(pf + "mod", objide.Mod.ToString()),
                                                                    new XElement(pf + "serie", objide.Serie.ToString()),
                                                                    new XElement(pf + "nNF", objide.Nnf.ToString()),
                                                                    new XElement(pf + "dEmi", objide.Demi.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "dSaiEnt", objide.Dsaient.ToString("yyyy-MM-dd")),
                                                                    new XElement(pf + "hSaiEnt", objide.HSaiEnt.ToString("HH:mm:ss")), // NFe_2.0
                                                                    new XElement(pf + "tpNF", objide.Tpnf.ToString()),
                                                                    new XElement(pf + "cMunFG", objide.Cmunfg.ToString()),
                                                                    (objide.belNFref != null ?
                                                                    (from c in objide.belNFref
                                                                     select new XElement(pf + "NFref",
                                                                                (c.RefNFe != null ? new XElement(pf + "refNFe", c.RefNFe) : null),
                                                                               (c.cUF != null ? (new XElement(pf + "refNF",
                                                                                (c.cUF != null ? new XElement(pf + "cUF", c.cUF) : null),
                                                                                 (c.AAMM != null ? new XElement(pf + "AAMM", c.AAMM) : null),
                                                                                 (c.CNPJ != null ? new XElement(pf + "CNPJ", c.CNPJ) : null),
                                                                                 (c.mod != null ? new XElement(pf + "mod", c.mod) : null),
                                                                                 (c.serie != null ? new XElement(pf + "serie", c.serie) : null),
                                                                                 (c.nNF != null ? new XElement(pf + "nNF", c.nNF) : null))) : null))) : null),//NFe_2.0_Verificar ID B14 - B20a - B20i - 
                                                                    new XElement(pf + "tpImp", objide.Tpimp.ToString()),
                                                                    new XElement(pf + "tpEmis", objide.Tpemis.ToString()),
                                                                    new XElement(pf + "cDV", objide.Cdv.ToString()),
                                                                    new XElement(pf + "tpAmb", objide.Tpamb.ToString()),
                                                                    new XElement(pf + "finNFe", objide.Finnfe.ToString()),
                                                                    new XElement(pf + "procEmi", objide.Procemi.ToString()),
                                                                    new XElement(pf + "verProc", objide.Verproc.ToString()),

                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) ?
                                                                                          new XElement(pf + "dhCont", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss")) : null), // NFe_2.0
                                                                    ((objide.Tpemis.Equals("2")) || (objide.Tpemis.Equals("3")) ?
                                                                                          new XElement(pf + "xJust", (iStatusAtualSistema == 2 ? "FALHA DE CONEXÃO COM INTERNET" : "FALHA COM WEB SERVICE DO ESTADO")) : null)));// NFe_2.0
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_ide - " + x.Message);
                    }
                    #endregion

                    #region Emit

                    XContainer conemit;
                    try
                    {
                        belEmit objemit = i[1] as belEmit;
                        #region XML_Emit

                        conemit = (new XElement(pf + "emit", (new XElement(pf + "CNPJ", objemit.Cnpj.ToString())),
                                                  new XElement(pf + "xNome", objemit.Xnome.ToString()),
                                                  new XElement(pf + "xFant", objemit.Xfant.ToString()),
                                                          new XElement(pf + "enderEmit",
                                                                  new XElement(pf + "xLgr", objemit.Xlgr.ToString()),
                                                                  new XElement(pf + "nro", objemit.Nro.ToString()),
                                                                  (objemit.Xcpl != null ? new XElement(pf + "xCpl", objemit.Xcpl.ToString()) : null),
                                                                  new XElement(pf + "xBairro", objemit.Xbairro.ToString()),
                                                                  new XElement(pf + "cMun", objemit.Cmun.ToString()),
                                                                  new XElement(pf + "xMun", objemit.Xmun.ToString()),
                                                                  new XElement(pf + "UF", objemit.Uf.ToString()),
                                                                  new XElement(pf + "CEP", objemit.Cep.ToString()),
                                                                  new XElement(pf + "cPais", objemit.Cpais.ToString()),
                                                                  new XElement(pf + "xPais", objemit.Xpais.ToString()),
                                                                  (objemit.Fone != null ? new XElement(pf + "fone", objemit.Fone.ToString()) : null)),
                                                  (objemit.Ie != null ? new XElement(pf + "IE", objemit.Ie.ToString()) : null),
                                                  (objemit.Iest != null ? new XElement(pf + "IEST", objemit.Iest.ToString()) : null),
                                                  (objemit.Im != null ? new XElement(pf + "IM", objemit.Im.ToString()) : null),
                                                  (objemit.Cnae != null ? new XElement(pf + "CNAE", objemit.Cnae.ToString()) : null),
                                                  new XElement(pf + "CRT", objemit.CRT.ToString()))); // NFe_2.0

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Emit - " + x.Message);
                    }
                    #endregion

                    #region Dest
                    XContainer condest;
                    try
                    {
                        belDest objdest = i[2] as belDest;
                        #region XML_Dest
                        objdest.Ie = (objdest.Ie == null ? "" : objdest.Ie);

                        condest = (new XElement(pf + "dest",
                                                  (objdest.Cnpj == "EXTERIOR" ? new XElement(pf + "CNPJ") :
                                                     (objdest.Cnpj != null ? new XElement(pf + "CNPJ", objdest.Cnpj) :
                                                                            new XElement(pf + "CPF", objdest.Cpf))),
                                                  new XElement(pf + "xNome", objdest.Xnome.ToString()),
                                                  new XElement(pf + "enderDest",
                                                      new XElement(pf + "xLgr", objdest.Xlgr.ToString()),
                                                      new XElement(pf + "nro", (objdest.Nro != null ? objdest.Nro.ToString() : "0")),
                                                      (objdest.Xcpl != null ? new XElement(pf + "xCpl", objdest.Xcpl.ToString()) : null),
                                                      new XElement(pf + "xBairro", objdest.Xbairro.ToString()),
                                                      new XElement(pf + "cMun", objdest.Cmun.ToString()),
                                                      new XElement(pf + "xMun", objdest.Xmun.ToString()),
                                                      new XElement(pf + "UF", objdest.Uf.ToString()),
                                                      (objdest.Cep != null ? new XElement(pf + "CEP", objdest.Cep.ToString()) : null),
                                                      new XElement(pf + "cPais", objdest.Cpais.ToString()),
                                                      (objdest.Xpais != null ? new XElement(pf + "xPais", objdest.Xpais.ToString()) : null),
                                                     (objdest.Fone != null ? new XElement(pf + "fone", objdest.Fone.ToString()) : null)),
                                                  ((objdest.Ie != null) ? (objdest.Ie != "EXTERIOR" ? new XElement(pf + "IE", objdest.Ie.ToString()) : new XElement(pf + "IE")) : null), //Claudinei - o.s. sem - 11/02/2010
                                                  (objdest.Isuf != null ? new XElement(pf + "ISUF", objdest.Isuf.ToString()) : null)));


                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Dest - " + x.Message);
                    }

                    #endregion

                    #region Det
                    List<XElement> lcondet = new List<XElement>();
                    try
                    {
                        List<belDet> objdet = new List<belDet>();
                        objdet = i[4] as List<belDet>;
                        #region XML_Detalhes

                        foreach (var det in objdet)
                        {
                            XElement condet = (new XElement(pf + "det", new XAttribute("nItem", det.Nitem),
                                               new XElement(pf + "prod",
                                                   (det.belProd.Cprod != null ? new XElement(pf + "cProd", det.belProd.Cprod.ToString()) : null),
                                                   (det.belProd.Cean != null ? new XElement(pf + "cEAN", det.belProd.Cean.ToString()) : null),
                                                   (det.belProd.Xprod != null ? new XElement(pf + "xProd", det.belProd.Xprod.ToString()) : null),
                                                   (det.belProd.Ncm != null ? new XElement(pf + "NCM", det.belProd.Ncm.ToString()) : new XElement(pf + "NCM", det.belProd.Ncm = "00000000")), //Claudinei - o.s. 24200 - 01/03/2010
                                                   (det.belProd.Extipi != null ? new XElement(pf + "EXTIPI", det.belProd.Extipi.ToString()) : null),
                                                   (det.belProd.Genero != null ? new XElement(pf + "genero", det.belProd.Genero.ToString()) : null),
                                                   (det.belProd.Cfop != null ? new XElement(pf + "CFOP", det.belProd.Cfop.ToString()) : null),
                                                   (det.belProd.Ucom != null ? new XElement(pf + "uCom", det.belProd.Ucom.ToString()) : null),
                                                   (det.belProd.Qcom != null ? new XElement(pf + "qCom", det.belProd.Qcom.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vuncom != null ? new XElement(pf + "vUnCom", det.belProd.Vuncom.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vprod != null ? new XElement(pf + "vProd", det.belProd.Vprod.ToString("#0.00").Replace(",", ".")) : null),
                                                   (det.belProd.Ceantrib != null ? new XElement(pf + "cEANTrib", det.belProd.Ceantrib.ToString().PadLeft(8, '0')) : null),
                                                   (det.belProd.Utrib != null ? new XElement(pf + "uTrib", det.belProd.Utrib.ToString()) : null),
                                                   (det.belProd.Qtrib != null ? new XElement(pf + "qTrib", det.belProd.Qtrib.ToString("#0.0000").Replace(",", ".")) : null),
                                                   (det.belProd.Vuntrib != null ? new XElement(pf + "vUnTrib", det.belProd.Vuntrib.ToString("#0.0000").Replace(",", ".")) : null),
                                                   ((det.belProd.Vfrete != null) && (det.belProd.Vfrete != 0) ? new XElement(pf + "vFrete", det.belProd.Vfrete.ToString("#0.00").Replace(",", ".")) : null),
                                                   ((det.belProd.Vseg != null) && (det.belProd.Vseg != 0) ? new XElement(pf + "vSeg", det.belProd.Vseg.ToString("#0.00").Replace(",", ".")) : null),
                                                   (((det.belProd.Vdesc != null) && (det.belProd.Vdesc != 0)) ? new XElement(pf + "vDesc", det.belProd.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                   ((det.belProd.VOutro != null) && (det.belProd.VOutro != 0) ? new XElement(pf + "vOutro", det.belProd.VOutro.ToString("#0.00").Replace(",", ".")) : null), //NFe_2.0 
                                                   (det.belProd.IndTot != null) ? new XElement(pf + "indTot", det.belProd.IndTot.ToString()) : null), //NFe_2.0

                                               new XElement(pf + "imposto",

                                                   //---------------ICMS-----------------//

                                                   new XElement(pf + "ICMS",

                                                       //-------------ICMS00-------------//

                                                       (det.belImposto.belIcms.belIcms00 != null ?
                                                       new XElement(pf + "ICMS00",
                                                            (det.belImposto.belIcms.belIcms00.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms00.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms00.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms00.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms00.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms00.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms00.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms00.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms00.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms00.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS10-------------//

                                                        (det.belImposto.belIcms.belIcms10 != null ?
                                                        new XElement(pf + "ICMS10",
                                                            (det.belImposto.belIcms.belIcms10.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms10.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms10.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms10.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms10.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms10.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms10.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms10.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms10.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms10.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 11/03/2010
                                                            (det.belImposto.belIcms.belIcms10.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms10.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms10.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms10.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms10.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms10.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS20-------------//

                                                        (det.belImposto.belIcms.belIcms20 != null ?
                                                        new XElement(pf + "ICMS20",
                                                            (det.belImposto.belIcms.belIcms20.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms20.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms20.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms20.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms20.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms20.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms20.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms20.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms20.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms20.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS30-------------//

                                                        (det.belImposto.belIcms.belIcms30 != null ?
                                                        new XElement(pf + "ICMS30",
                                                            (det.belImposto.belIcms.belIcms30.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms30.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms30.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms30.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms30.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms30.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms30.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms30.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms30.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms30.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms30.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms30.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS40-------------//

                                                        (det.belImposto.belIcms.belIcms40 != null ?
                                                        new XElement(pf + "ICMS40",
                                                            (det.belImposto.belIcms.belIcms40.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms40.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms40.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms40.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms40.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms40.Vicms.ToString()) : null), //NFe_2.0
                                                            (det.belImposto.belIcms.belIcms40.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms40.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS41-------------//

                                                        (det.belImposto.belIcms.belIcms41 != null ?
                                                        new XElement(pf + "ICMS41",
                                                            (det.belImposto.belIcms.belIcms41.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms41.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms41.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms41.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms41.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms41.Vicms.ToString()) : null),//NFe_2.0
                                                            (det.belImposto.belIcms.belIcms41.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms41.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS50-------------//

                                                        (det.belImposto.belIcms.belIcms50 != null ?
                                                        new XElement(pf + "ICMS50",
                                                            (det.belImposto.belIcms.belIcms50.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms50.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms50.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms50.Cst.ToString()) : null),
                                                                (det.belImposto.belIcms.belIcms50.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms50.Vicms.ToString()) : null),//NFe_2.0
                                                            (det.belImposto.belIcms.belIcms50.motDesICMS != null ? new XElement(pf + "motDesICMS", det.belImposto.belIcms.belIcms50.motDesICMS.ToString()) : null)) : null),//NFe_2.0

                                                        //-------------ICMS51-------------//

                                                        (det.belImposto.belIcms.belIcms51 != null ?
                                                        new XElement(pf + "ICMS51",
                                                            (det.belImposto.belIcms.belIcms51.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms51.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms51.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms51.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms51.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms51.Predbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms51.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms51.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms51.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms51.Vicms.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS60-------------//

                                                        (det.belImposto.belIcms.belIcms60 != null ?
                                                        new XElement(pf + "ICMS60",//Danner - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms60.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms60.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms60.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms60.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms60.Vbcst != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belIcms60.Vbcst.ToString("#0.00").Replace(",", ".")) : null),//NFe_2.0 - Mudança de nome de Tag
                                                            (det.belImposto.belIcms.belIcms60.Vicmsst != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belIcms60.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),//NFe_2.0 Mudança de nome de Tag

                                                        //-------------ICMS70-------------//

                                                        (det.belImposto.belIcms.belIcms70 != null ?
                                                        new XElement(pf + "ICMS70",
                                                            (det.belImposto.belIcms.belIcms70.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms70.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms70.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms70.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Predbc != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms70.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010
                                                            (det.belImposto.belIcms.belIcms70.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms70.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms70.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms70.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms70.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms70.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms70.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. sem - 12/03/2010
                                                            (det.belImposto.belIcms.belIcms70.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms70.Predbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms70.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms70.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms70.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms70.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                        //-------------ICMS90-------------//

                                                        (det.belImposto.belIcms.belIcms90 != null ?
                                                        new XElement(pf + "ICMS90",
                                                            (det.belImposto.belIcms.belIcms90.Orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belIcms90.Orig.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Cst != null ? new XElement(pf + "CST", det.belImposto.belIcms.belIcms90.Cst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Modbc != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belIcms90.Modbc.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belIcms90.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Predbc != 0 ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belIcms90.Predbc.ToString("#0.00").Replace(',', '.')) : null), //Danner - o.s. 24091 - 06/02/2010 //Claudinei - o.s. sem - 24/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Picms != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belIcms90.Picms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vicms != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belIcms90.Vicms.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Modbcst != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belIcms90.Modbcst.ToString()) : null),
                                                            (det.belImposto.belIcms.belIcms90.Pmvast != 0 ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belIcms90.Pmvast.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Predbcst.ToString() != "0" ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belIcms90.Predbcst.ToString("#0.00").Replace(",", ".")) : null), //Claudinei - o.s. 24076 - 01/02/2010
                                                            (det.belImposto.belIcms.belIcms90.Vbcst != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belIcms90.Vbcst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Picmsst != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belIcms90.Picmsst.ToString("#0.00").Replace(",", ".")) : null),
                                                            (det.belImposto.belIcms.belIcms90.Vicmsst != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belIcms90.Vicmsst.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                       //-------------ICMSSN101--------------//
                                                       (det.belImposto.belIcms.belICMSSN101 != null ?
                                                            new XElement(pf + "ICMSSN101",
                                                               (det.belImposto.belIcms.belICMSSN101.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN101.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN101.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN101.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN101.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN101.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN101.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN102--------------//
                                                       (det.belImposto.belIcms.belICMSSN102 != null ?
                                                            new XElement(pf + "ICMSSN102",
                                                               (det.belImposto.belIcms.belICMSSN102.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN102.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN102.CSOSN.ToString())) : null),

                                                      //-------------ICMSSN201--------------//
                                                       (det.belImposto.belIcms.belICMSSN201 != null ?
                                                            new XElement(pf + "ICMSSN201",
                                                               (det.belImposto.belIcms.belICMSSN201.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN201.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN201.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN201.modBCST != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belICMSSN201.modBCST.ToString()) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pMVAST != null ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belICMSSN201.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pRedBCST != null ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belICMSSN201.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vBCST != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belICMSSN201.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pICMSST != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belICMSSN201.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vICMSST != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belICMSSN201.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN201.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN201.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN201.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN500--------------//
                                                       (det.belImposto.belIcms.belICMSSN500 != null ?
                                                            new XElement(pf + "ICMSSN500",
                                                               (det.belImposto.belIcms.belICMSSN500.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN500.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN500.CSOSN.ToString()),
                                                               (det.belImposto.belIcms.belICMSSN500.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belICMSSN500.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                                               (det.belImposto.belIcms.belICMSSN500.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belICMSSN500.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                      //-------------ICMSSN900--------------//
                                                       (det.belImposto.belIcms.belICMSSN900 != null ?
                                                            new XElement(pf + "ICMSSN900",
                                                               (det.belImposto.belIcms.belICMSSN900.orig != null ? new XElement(pf + "orig", det.belImposto.belIcms.belICMSSN900.orig.ToString()) : null),
                                                               new XElement(pf + "CSOSN", det.belImposto.belIcms.belICMSSN900.CSOSN.ToString())) : null)),
                                //(det.belImposto.belIcms.belICMSSN900.modBC != null ? new XElement(pf + "modBC", det.belImposto.belIcms.belICMSSN900.modBC.ToString()) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBC != null ? new XElement(pf + "vBC", det.belImposto.belIcms.belICMSSN900.vBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pRedBC != null ? new XElement(pf + "pRedBC", det.belImposto.belIcms.belICMSSN900.pRedBC.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pICMS != null ? new XElement(pf + "pICMS", det.belImposto.belIcms.belICMSSN900.pICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMS != null ? new XElement(pf + "vICMS", det.belImposto.belIcms.belICMSSN900.vICMS.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.modBCST != null ? new XElement(pf + "modBCST", det.belImposto.belIcms.belICMSSN900.modBCST.ToString()) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pMVAST != null ? new XElement(pf + "pMVAST", det.belImposto.belIcms.belICMSSN900.pMVAST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pRedBCST != null ? new XElement(pf + "pRedBCST", det.belImposto.belIcms.belICMSSN900.pRedBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBCST != null ? new XElement(pf + "vBCST", det.belImposto.belIcms.belICMSSN900.vBCST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pICMSST != null ? new XElement(pf + "pICMSST", det.belImposto.belIcms.belICMSSN900.pICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMSST != null ? new XElement(pf + "vICMSST", det.belImposto.belIcms.belICMSSN900.vICMSST.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vBCSTRet != null ? new XElement(pf + "vBCSTRet", det.belImposto.belIcms.belICMSSN900.vBCSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vICMSSTRet != null ? new XElement(pf + "vICMSSTRet", det.belImposto.belIcms.belICMSSN900.vICMSSTRet.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.pCredSN != null ? new XElement(pf + "pCredSN", det.belImposto.belIcms.belICMSSN900.pCredSN.ToString("#0.00").Replace(",", ".")) : null),
                                //(det.belImposto.belIcms.belICMSSN900.vCredICMSSN != null ? new XElement(pf + "vCredICMSSN", det.belImposto.belIcms.belICMSSN900.vCredICMSSN.ToString("#0.00").Replace(",", ".")) : null)) : null)),

                                        //---------------IPI-------------//
                                            (det.belImposto.belIpi != null ?
                                            new XElement(pf + "IPI",
                                                (det.belImposto.belIpi.Clenq != null ?
                                                    new XElement(pf + "clEnq", det.belImposto.belIpi.Clenq.ToString()) : null),
                                                (det.belImposto.belIpi.Cnpjprod != null ?
                                                    new XElement(pf + "CNPJProd", det.belImposto.belIpi.Cnpjprod.ToString()) : null),
                                                (det.belImposto.belIpi.Cselo != null ?
                                                    new XElement(pf + "cSelo", det.belImposto.belIpi.Cselo.ToString()) : null),
                                                (det.belImposto.belIpi.Qselo != null ?
                                                    new XElement(pf + "qSelo", det.belImposto.belIpi.Qselo.ToString()) : null),
                                                (det.belImposto.belIpi.Cenq != null ?
                                                    new XElement(pf + "cEnq", det.belImposto.belIpi.Cenq.ToString()) : null),

                                                //-----------IPITrib-----------//    

                                                (det.belImposto.belIpi.belIpitrib != null ?
                                                new XElement(pf + "IPITrib",
                                                    (det.belImposto.belIpi.belIpitrib.Cst != null ? new XElement(pf + "CST", det.belImposto.belIpi.belIpitrib.Cst.ToString()) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIpi.belIpitrib.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Qunid != null ? new XElement(pf + "qUnid", det.belImposto.belIpi.belIpitrib.Qunid.ToString()) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vunid != 0 ? new XElement(pf + "vUnid", det.belImposto.belIpi.belIpitrib.Vunid.ToString("#0.0000").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Pipi != null ? new XElement(pf + "pIPI", det.belImposto.belIpi.belIpitrib.Pipi.ToString("#0.00").Replace(",", ".")) : null),
                                                    (det.belImposto.belIpi.belIpitrib.Vipi != null ? new XElement(pf + "vIPI", det.belImposto.belIpi.belIpitrib.Vipi.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                                //-----------IPINT-----------//

                                                (det.belImposto.belIpi.belIpint != null ?
                                                new XElement(pf + "IPINT",
                                                    (det.belImposto.belIpi.belIpint != null ? new XElement(pf + "CST", det.belImposto.belIpi.belIpint.Cst.ToString()) : null)) : null)) : null),




                                       //--------------II--------------//             
                                       (det.belImposto.belIi != null ?
                                       new XElement(pf + "II",
                                           (det.belImposto.belIi.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIi.Vbc.ToString("#0.00").Replace(',', '.')) : null),
                                           (det.belImposto.belIi.Vdespadu != null ? new XElement(pf + "vDespAdu", det.belImposto.belIi.Vdespadu.ToString()) : null),
                                           (det.belImposto.belIi.Vii != null ? new XElement(pf + "vII", det.belImposto.belIi.Vii.ToString("0.00").Replace(',', '.')) : null),
                                           (det.belImposto.belIi.Viof != null ? new XElement(pf + "vIOF", det.belImposto.belIi.Viof.ToString("#0.00").Replace(',', '.')) : null)) : null),



                               //----------------PIS------------//

                                 (det.belImposto.belPis != null ?
                                 new XElement(pf + "PIS",

                                     //-----------PISAliq----------//

                                     (det.belImposto.belPis.belPisaliq != null ?
                                     new XElement(pf + "PISAliq",
                                         (det.belImposto.belPis.belPisaliq.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisaliq.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisaliq.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belPis.belPisaliq.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisaliq.Ppis != null ? new XElement(pf + "pPIS", det.belImposto.belPis.belPisaliq.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisaliq.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisaliq.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                    //-----------PISQtde-----------//                               
                                     (det.belImposto.belPis.belPisqtde != null ?
                                     new XElement(pf + "PISQtde",
                                         (det.belImposto.belPis.belPisqtde.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisqtde.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisqtde.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.belImposto.belPis.belPisqtde.Qbcprod.ToString()) : null),
                                         (det.belImposto.belPis.belPisqtde.Valiqprod != null ? new XElement(pf + "vAliqProd", det.belImposto.belPis.belPisqtde.Valiqprod.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisqtde.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisqtde.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null),

                                     //----------PISNT------------//

                                     (det.belImposto.belPis.belPisnt != null ?
                                     new XElement(pf + "PISNT",
                                         (det.belImposto.belPis.belPisnt.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisnt.Cst.ToString()) : null)) : null),

                                     //----------PISOutr-----------//

                                     (det.belImposto.belPis.belPisoutr != null ?
                                     new XElement(pf + "PISOutr",
                                         (det.belImposto.belPis.belPisoutr.Cst != null ? new XElement(pf + "CST", det.belImposto.belPis.belPisoutr.Cst.ToString()) : null),
                                         (det.belImposto.belPis.belPisoutr.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belPis.belPisoutr.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Ppis != null ? new XElement(pf + "pPIS", det.belImposto.belPis.belPisoutr.Ppis.ToString("#0.00").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Qbcprod != 0 ? new XElement(pf + "qBCProd", det.belImposto.belPis.belPisoutr.Qbcprod.ToString()) : null),
                                         (det.belImposto.belPis.belPisoutr.Valiqprod != 0 ? new XElement(pf + "vAliqProd", det.belImposto.belPis.belPisoutr.Valiqprod.ToString("#0.0000").Replace(",", ".")) : null),
                                         (det.belImposto.belPis.belPisoutr.Vpis != null ? new XElement(pf + "vPIS", det.belImposto.belPis.belPisoutr.Vpis.ToString("#0.00").Replace(",", ".")) : null)) : null)) : null),







                                 //---------------COFINS---------------//
                                 (det.belImposto.belCofins != null ?
                                 new XElement(pf + "COFINS",

                                     //-----------COFINSAliq------------//

                                     (det.belImposto.belCofins.belCofinsaliq != null ?
                                     new XElement(pf + "COFINSAliq",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsaliq.Cst.ToString()),
                                         new XElement(pf + "vBC", det.belImposto.belCofins.belCofinsaliq.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.belImposto.belCofins.belCofinsaliq.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsaliq.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSQtde------------//

                                     (det.belImposto.belCofins.belCofinsqtde != null ?
                                     new XElement(pf + "COFINSQtde",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsqtde.Cst.ToString()),
                                         new XElement(pf + "pBCProd", det.belImposto.belCofins.belCofinsqtde.Qbcprod.ToString()),
                                         new XElement(pf + "vAliqProd", det.belImposto.belCofins.belCofinsqtde.Valiqprod.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsqtde.Vcofins.ToString("#0.00").Replace(",", "."))) : null),

                                     //------------COFINSNT--------------//

                                     (det.belImposto.belCofins.belCofinsnt != null ?
                                     new XElement(pf + "COFINSNT",
                                         (det.belImposto.belCofins.belCofinsnt.Cst != null ? new XElement(pf + "CST", det.belImposto.belCofins.belCofinsnt.Cst.ToString()) : null)) : null),

                                     //------------COFINSOutr--------------//

                                     (det.belImposto.belCofins.belCofinsoutr != null ?
                                     new XElement(pf + "COFINSOutr",
                                         new XElement(pf + "CST", det.belImposto.belCofins.belCofinsoutr.Cst.ToString()),
                                         new XElement(pf + "vBC", det.belImposto.belCofins.belCofinsoutr.Vbc.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "pCOFINS", det.belImposto.belCofins.belCofinsoutr.Pcofins.ToString("#0.00").Replace(",", ".")),
                                         new XElement(pf + "vCOFINS", det.belImposto.belCofins.belCofinsoutr.Vcofins.ToString("#0.00").Replace(",", "."))) : null)) : null),


                                 //----------------ISSQN-----------------//

                                 (det.belImposto.belIss != null ?
                                 new XElement(pf + "ISSQN",
                                     (det.belImposto.belIss.Vbc != null ? new XElement(pf + "vBC", det.belImposto.belIss.Vbc.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Valiq != null ? new XElement(pf + "vAliq", det.belImposto.belIss.Valiq.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Vissqn != null ? new XElement(pf + "vISSQN", det.belImposto.belIss.Vissqn.ToString("#0.00").Replace(",", ".")) : null),
                                     (det.belImposto.belIss.Cmunfg != null ? new XElement(pf + "cMunFG", det.belImposto.belIss.Cmunfg.ToString()) : null),
                                     (det.belImposto.belIss.Clistserv != null ? new XElement(pf + "cListServ", det.belImposto.belIss.Clistserv.ToString()) : null),
                                     (det.belImposto.belIss.cSitTrib != null ? new XElement(pf + "cSitTrib", det.belImposto.belIss.cSitTrib.ToString()) : null)) : null)), // NFe_2.0 Tratar item


                                 //-----------INFADPROD-------------//

                                 (det.belInfadprod != null ?
                                 (det.belInfadprod.Infadprid != null ? new XElement(pf + "infAdProd", det.belInfadprod.Infadprid.ToString()) : null) : null))); //Danner - o.s. sem -21/12/2009

                            lcondet.Add(condet);
                        }
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Detalhes - " + x.Message);
                    }
                    #endregion

                    //Total
                    XContainer contotal;

                    try
                    {
                        belTotal objtotal = i[5] as belTotal;
                        #region XML_Total

                        contotal = (new XElement(pf + "total",
                                                (objtotal.belIcmstot != null ? new XElement(pf + "ICMSTot",
                                                      new XElement(pf + "vBC", objtotal.belIcmstot.Vbc.ToString("#0.00").Replace(",", ".")),//Danner - o.s. 24271 - 15/03/2010
                                                      new XElement(pf + "vICMS", objtotal.belIcmstot.Vicms.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vBCST", objtotal.belIcmstot.Vbcst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vST", objtotal.belIcmstot.Vst.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vProd", objtotal.belIcmstot.Vprod.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vFrete", objtotal.belIcmstot.Vfrete.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vSeg", objtotal.belIcmstot.Vseg.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vDesc", objtotal.belIcmstot.Vdesc.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vII", objtotal.belIcmstot.Vii.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vIPI", objtotal.belIcmstot.Vipi.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vPIS", objtotal.belIcmstot.Vpis.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vCOFINS", objtotal.belIcmstot.Vcofins.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vOutro", objtotal.belIcmstot.Voutro.ToString("#0.00").Replace(",", ".")),
                                                      new XElement(pf + "vNF", objtotal.belIcmstot.Vnf.ToString("#0.00").Replace(",", "."))) : null),



                                                (objtotal.belIssqntot != null ? new XElement(pf + "ISSQNtot",
                                                    new XElement(pf + "vServ", objtotal.belIssqntot.Vserv.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vBC", objtotal.belIssqntot.Vbc.ToString("#0.00").Replace(",", ".")),
                                                    new XElement(pf + "vISS", objtotal.belIssqntot.Viss.ToString("#0.00").Replace(",", ".")),
                                                    (objtotal.belIssqntot.Vpis != 0 ? new XElement(pf + "vPIS", objtotal.belIssqntot.Vpis.ToString("#0.00").Replace(",", ".")) : null),
                                                    (objtotal.belIssqntot.Vcofins != 0 ? new XElement(pf + "vCOFINS", objtotal.belIssqntot.Vcofins.ToString("#0.00").Replace(",", ".")) : null)) : null)));
                        //(objtotal.belRetTrib != null ? new XElement(pf + "retTrib",
                        //                                   new XElement(pf + "vRetPIS", objtotal.belRetTrib.Vretpis.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetCOFINS", objtotal.belRetTrib.Vretcofins.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetCSLL", objtotal.belRetTrib.Vretcsll.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vBCIRRF", objtotal.belRetTrib.Vbcirrf.ToString("#0.00"),
                        //                                   new XElement(pf + "vIRRF", objtotal.belRetTrib.Virrf.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vBCRetPrev", objtotal.belRetTrib.Vbcretprev.ToString("#0.00").Replace(",", ".")),
                        //                                   new XElement(pf + "vRetPrev", objtotal.belRetTrib.Vretprev.ToString("#0.00").Replace(",", "."))) : null)));

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Total - " + x.Message);
                    }
                    //Fim - Total

                    //Frete
                    XContainer contransp;
                    belTransp objtransp;
                    try
                    {
                        objtransp = i[6] as belTransp;
                        #region XML_Transporte

                        contransp = (new XElement(pf + "transp",
                                                   new XElement(pf + "modFrete", objtransp.Modfrete.ToString()),
                                                   new XElement(pf + "transporta",
                                                       (objtransp.belTransportadora.Cnpj != null ? new XElement(pf + "CNPJ", objtransp.belTransportadora.Cnpj.ToString()) :
                                                                                                   (objtransp.belTransportadora.Cpf != null ? new XElement(pf + "CPF", objtransp.belTransportadora.Cpf.ToString()) : null)),
                                                       (objtransp.belTransportadora.Xnome != null ? new XElement(pf + "xNome", objtransp.belTransportadora.Xnome.ToString()) : null),
                                                       (objtransp.belTransportadora.Ie != null ? new XElement(pf + "IE", objtransp.belTransportadora.Ie.ToString()) : null),
                                                       (objtransp.belTransportadora.Xender != null ? new XElement(pf + "xEnder", objtransp.belTransportadora.Xender.ToString()) : null),
                                                       (objtransp.belTransportadora.Xmun != null ? new XElement(pf + "xMun", objtransp.belTransportadora.Xmun.ToString()) : null),
                                                       (objtransp.belTransportadora.Uf != null ? new XElement(pf + "UF", objtransp.belTransportadora.Uf.ToString()) : null)),
                (objtransp.belRetTransp != null ? (new XElement(pf + "retTransp",
                                                       new XElement(pf + "vServ", objtransp.belRetTransp.Vserv.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "vBCRet", objtransp.belRetTransp.Vbvret.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "pICMSRet", objtransp.belRetTransp.Picmsret.ToString()),
                                                       new XElement(pf + "vICMSRet", objtransp.belRetTransp.Vicmsret.ToString("#0.00").Replace(',', '.')),
                                                       new XElement(pf + "CFOP", objtransp.belRetTransp.Cfop.ToString()),
                                                       new XElement(pf + "cMunFG", objtransp.belRetTransp.Cmunfg.ToString()))) : null),
                (objtransp.belVeicTransp != null ? (new XElement(pf + "veicTransp",
                                                       (objtransp.belVeicTransp.Placa != null ? new XElement(pf + "placa", objtransp.belVeicTransp.Placa.ToString()) : null),
                                                       (objtransp.belVeicTransp.Uf != null ? new XElement(pf + "UF", objtransp.belVeicTransp.Uf.ToString()) : null),
                                                       (objtransp.belVeicTransp.Rntc != null ? new XElement(pf + "RNTC", objtransp.belVeicTransp.Rntc.ToString()) : null))) : null),
                   (objtransp.belReboque != null ? new XElement(pf + "reboque",
                                                       (objtransp.belReboque.Placa != null ? new XElement(pf + "placa", objtransp.belReboque.Placa.ToString()) : null),
                                                       (objtransp.belReboque.Uf != null ? new XElement(pf + "UF", objtransp.belReboque.Uf.ToString()) : null),
                                                       (objtransp.belReboque.Rntc != null ? new XElement(pf + "RNTC", objtransp.belReboque.Rntc.ToString()) : null)) : null),
                       (objtransp.belVol != null ? new XElement(pf + "vol",
                                                       new XElement(pf + "qVol", objtransp.belVol.Qvol.ToString("#")),
                                                       new XElement(pf + "esp", objtransp.belVol.Esp.ToString()),
                                                       new XElement(pf + "marca", objtransp.belVol.Marca.ToString()),
                                                       (objtransp.belVol.Nvol != null ? new XElement(pf + "nVol", objtransp.belVol.Nvol.ToString()) : null),//Danner - o.s. 24385 - 26/04/2010
                                                       new XElement(pf + "pesoL", objtransp.belVol.PesoL.ToString("#0.000").Replace(",", ".")),
                                                       new XElement(pf + "pesoB", objtransp.belVol.PesoB.ToString("#0.000").Replace(",", "."))) : null),
                   (objtransp.belLacres != null ? new XElement(pf + "lacres",
                                                       new XElement(pf + "nLacre", "")) : null)));



                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Regiao XML_Transporte - " + x.Message);
                    }
                    //Fim - Frete

                    //Duplicata
                    XContainer concobr;
                    belCobr objcob;
                    try
                    {
                        objcob = i[7] as belCobr;
                        belGerarXML BuscaConexao = new belGerarXML();
                        #region XML_Cobrança
                        concobr = (new XElement(pf + "cobr",
                                                  new XElement(pf + "fat",
                                                      new XElement(pf + "nFat", objcob.belFat.Nfat.ToString()),
                                                      (objcob.belFat.Vorig != 0 ? new XElement(pf + "vOrig", objcob.belFat.Vorig.ToString("#0.00").Replace(",", ".")) : null),
                                                      (objcob.belFat.Vdesc != null && objcob.belFat.Vdesc != 0 ? new XElement(pf + "vDesc", objcob.belFat.Vdesc.ToString("#0.00").Replace(",", ".")) : null),
                                                      (objcob.belFat.Vliq != 0 ? new XElement(pf + "vLiq", objcob.belFat.Vliq.ToString("#0.00").Replace(",", ".")) : null)),
                                                      (objcob.belFat.belDup != null ? from dup in objcob.belFat.belDup
                                                                                      select new XElement(pf + "dup", new XElement(pf + "nDup", dup.Ndup.ToString()),
                                                                                             new XElement(pf + "dVenc", dup.Dvenc.ToString("yyyy-MM-dd")),
                                                                                             (BuscaConexao.nm_Cliente != "LORENZON" ? new XElement(pf + "vDup", dup.Vdup.ToString("#0.00").Replace(",", ".")) : null)) : null)));

                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML, Region XML_Cobrança - " + x.Message);
                    }
                    //Fim - Duplicata

                    //Obs
                    XContainer conobs;
                    belInfAdic objobs;
                    try
                    {
                        objobs = i[8] as belInfAdic;
                        #region XML_Obs
                        conobs = new XElement(pf + "infAdic",
                                                    (objobs.Infcpl != null ?
                                                    new XElement(pf + "infCpl", objobs.Infcpl.ToString()) : null));
                        #endregion
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na geração do XML,Regiao XML_Obs - " + x.Message);
                    }
                    //Fim - Obs

                    //Uniao dos Containers
                    try
                    {
                        concabec.Add(coninfnfe);
                        coninfnfe.Add(conide);
                        conide.AddAfterSelf(conemit);
                        conemit.AddAfterSelf(condest);
                        condest.AddAfterSelf(contotal);
                        contotal.AddAfterSelf(contransp);

                        if (concobr != null)
                        {
                            contransp.AddAfterSelf(concobr);
                            if (objobs.Infcpl != null)
                                concobr.AddAfterSelf(conobs);
                        }
                        else
                        {
                            if (objobs.Infcpl != null)
                                contransp.AddAfterSelf(conobs);
                        }

                        foreach (XElement x in lcondet)
                        {
                            contotal.AddBeforeSelf(x);
                        }
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro na montagem do XML, União dos Containers - " + x.Message);
                    }
                    try
                    {
                        AssinaNFeXml Assinatura = new AssinaNFeXml();
                        string nfe = Assinatura.ConfigurarArquivo(concabec.ToString(), "infNFe", cert);
                        nfes.Add(nfe);
                        XElement xnfe = XElement.Parse(nfe);
                        XDocument xdocsalvanfesemlot = new XDocument(xnfe);

                        DirectoryInfo dPastaData = new DirectoryInfo(glob.LeRegConfig("PastaXmlEnvio").ToString() + "\\" + sNFe.Substring(5, 4));
                        if (!dPastaData.Exists) { dPastaData.Create(); }
                        if (sFormaEmiNFe.Equals("2"))
                        {
                            xdocsalvanfesemlot.Save(glob.LeRegConfig("PastaContingencia").ToString() + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml");
                        }
                        else
                        {
                            xdocsalvanfesemlot.Save(glob.LeRegConfig("PastaXmlEnvio").ToString() + "\\" + sNFe.Substring(5, 4) + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml"); // OS_25024
                        }
                        //StreamWriter swnfe = new StreamWriter(glob.LeRegWin("PastaXmlEnvio").ToString() + "\\" + sNFe.Replace("NFe", "") + "-nfe.xml");
                        //swnfe.Write(nfe);
                        //swnfe.Close();
                    }
                    catch (Exception x)
                    {
                        throw new Exception("Nota de Sequência - " + sNota + "Erro ao assinar a nfe de sequencia " + sNota + x.Message);
                    }

                    iCount++;
                }

                //Concatenando as NFes.
                string sXmlComp = "";
                //Junta todos os XML's em texto 
                foreach (var i in nfes)
                {
                    sXmlComp = sXmlComp + i;
                }
                //Estou inserindo o enviNFe pois se eu transformar o arquivo xml assinado em xml e depois em testo usando um toString,
                //a assinatura se torna invalida. Então depois de assinado do trabalho em forma de testo como xml ateh envia-lo pra fazenda.                        
                sXmlfull = "<?xml version=\"1.0\" encoding=\"utf-8\"?><enviNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"2.00\"><idLote>" +
                            sNomeArq.Substring(7, 15) + "</idLote>" + sXmlComp + "</enviNFe>";

                //Grava
                StreamWriter sw = new StreamWriter(sPath);
                sw.Write(sXmlfull);
                sw.Close();

                #region Valida_Xml

                Globais getschema = new Globais();

                //string sXml = File.OpenText(sPath).ReadToEnd();

                XmlSchemaCollection myschema = new XmlSchemaCollection();

                XmlValidatingReader reader;

                //Danner - o.s. 23732 - 06/11/2009

                try
                {
                    XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                    reader = new XmlValidatingReader(sXmlfull, XmlNodeType.Element, context);

                    myschema.Add("http://www.portalfiscal.inf.br/nfe", getschema.LeRegConfig("PastaSchema") + "\\enviNFe_v2.00.xsd");

                    reader.ValidationType = ValidationType.Schema;

                    reader.Schemas.Add(myschema);

                    while (reader.Read())
                    {

                    }

                }
                catch (XmlException x)
                {
                    File.Delete(sPath);
                    throw new Exception(x.Message);
                }
                catch (XmlSchemaException x)
                {
                    File.Delete(sPath);
                    throw new Exception(x.Message);
                }

                //Fim - Danner - o.s. 23732 - 06/11/2009

                #endregion

            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
            finally { Conn.Close(); }
        }