Foros del Web » Programación para mayores de 30 ;) » .NET »

[SOLUCIONADO] como agregar mas nodos "concepto" en un ciclo for??

Estas en el tema de como agregar mas nodos "concepto" en un ciclo for?? en el foro de .NET en Foros del Web. hola programadores necesito saber como agregar mas conceptos para el cfdi xml, por ejemplo para agregar estos 3 conceptos. <cfdi:Conceptos> <cfdi:Concepto cantidad="1.00" unidad="Pieza" noIdentificacion="Butterfly4" descripcion="Butterfly ...
  #1 (permalink)  
Antiguo 29/01/2014, 21:45
 
Fecha de Ingreso: enero-2014
Mensajes: 14
Antigüedad: 10 años, 3 meses
Puntos: 1
como agregar mas nodos "concepto" en un ciclo for??

hola programadores necesito saber como agregar mas conceptos para el cfdi xml, por ejemplo para agregar estos 3 conceptos.

<cfdi:Conceptos>
<cfdi:Concepto cantidad="1.00" unidad="Pieza" noIdentificacion="Butterfly4" descripcion="Butterfly Valve 4in 175 PSI Ductile iron wafer body buna-n seats ductile iron Weco(FMC) 3245819" valorUnitario="190.0000" importe="190.0000" />
<cfdi:Concepto cantidad="1.00" unidad="Pieza" noIdentificacion="Butterfly4" descripcion="Butterfly Valve 4in 175 PSI Ductile iron wafer body buna-n seats ductile iron Weco(FMC) 3245819" valorUnitario="190.0000" importe="190.0000" />
<cfdi:Concepto cantidad="1.00" unidad="Pieza" noIdentificacion="Butterfly4" descripcion="Butterfly Valve 4in 175 PSI Ductile iron wafer body buna-n seats ductile iron Weco(FMC) 3245819" valorUnitario="190.0000" importe="190.0000" />
</cfdi:Conceptos>

dejo el codigo en c# trabajo la creacion del xml con inq
public static void CrearArchivoCFDI()
{
try
{
string selloDigital_Certificado = "resultado";
string otro = "otro";
string Resultado = CrearSelloDigitalEmisor(ref selloDigital_Certificado, ref otro);

using (var context = new PL_Datos.DatosBDContext())
{
PL_Datos.PLcat_Cotizacion cotizacion = (from co in context.PLcat_Cotizacion
where co.Id_PLcatCotizacion == 1
select co).Single();
foreach (PL_Datos.PLtrn_PartidasCotizacion partidas in cotizacion.PLtrn_PartidasCotizacion)
{
}
XNamespace cfdi = XNamespace.Get("http://www.sat.gob.mx/cfd/3");
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XDocument xmlFactura = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("DATOS DE FACTURACION ELECTRONICA"),
new XElement(cfdi + "Comprobante",
new XAttribute(XNamespace.Xmlns + "cfdi", "http://www.sat.gob.mx/cfd/3"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(xsi + "schemaLocation", "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd"),
new XAttribute("version", "3.2"),
new XAttribute("serie", ""),
new XAttribute("folio", "00001"),
new XAttribute("fecha", ""),
new XAttribute("sello", ""),
new XAttribute("formaDePago", ""),
new XAttribute("noCertificado", ""),
new XAttribute("certificado", ""),
new XAttribute("condicionesDePago", ""),
new XAttribute("subTotal", ""),
new XAttribute("TipoCambio", ""),
new XAttribute("Moneda", ""),
new XAttribute("total", ""),
new XAttribute("metodoDePago", ""),
new XAttribute("tipoDeComprobante", ""),
new XAttribute("LugarExpedicion", "Ciudad del Carmen, Campeche, México"),
new XElement(cfdi + "Emisor",
new XAttribute("rfc", "GAIL810405RU6"),
new XAttribute("nombre", "LAZARO GARCIA IZQUIERDO"),
new XElement(cfdi + "DomicilioFiscal",
new XAttribute("calle", "40"),
new XAttribute("noExterior", "85"),
new XAttribute("colonia", "CUAUHTEMOC CIUDAD DEL CAMREN"),
new XAttribute("municipio", "CARMEN"),
new XAttribute("estado", "CAMPECHE"),
new XAttribute("pais", "MÉXICO"),
new XAttribute("codigoPostal", "24170")),
new XElement(cfdi + "RegimenFiscal",
new XAttribute("Regimen", ""))),
new XElement(cfdi + "Receptor",
new XAttribute("rfc", ""),
new XAttribute("nombre", ""),
new XElement(cfdi + "Domicilio",
new XAttribute("pais", ""),
new XAttribute("calle", ""),
new XAttribute("estado", ""),
new XAttribute("colonia", ""),
new XAttribute("municipio", ""),
new XAttribute("noExterior", ""),
new XAttribute("noInterior", ""),
new XAttribute("codigoPostal", ""))),
new XElement(cfdi + "Conceptos",
new XElement(cfdi + "Concepto",
new XAttribute("cantidad", ""),
new XAttribute("unidad", ""),
new XAttribute("noIdentificacion", ""),
new XAttribute("descripcion", ""),
new XAttribute("valorUnitario", ""),
new XAttribute("importe", ""))),
new XElement(cfdi + "Impuestos",
new XAttribute("totalImpuestosTrasladados", ""),
new XElement(cfdi + "Traslados",
new XElement(cfdi + "Traslado",
new XAttribute("impuesto", "IVA"),
new XAttribute("tasa", ""),
new XAttribute("importe", "")))),
new XElement(cfdi + "Complemento")));

xmlFactura.Save(@"c:\PartesEnLinea\PL_Presentacion \facturacion\prueba.xml");
}
}
catch (Exception ex)
{
throw ex;
}
}
espero proporcionen ayuda
saludos, Grcaias.
  #2 (permalink)  
Antiguo 30/01/2014, 09:43
Avatar de Namiwis  
Fecha de Ingreso: mayo-2013
Ubicación: Crustaceo Cascarudo
Mensajes: 162
Antigüedad: 10 años, 11 meses
Puntos: 10
Respuesta: como agregar mas nodos "concepto" en un ciclo for??

Hola rudolfcruz nuevamente

Te comparto como lo hago yo, aunque lo hago de manera distinta a como lo expones en tu codigo, espero te sirva para darte una idea

Código:
 W.WriteStartElement("cfdi:Conceptos");
            //Nodo Conceptos
            for (int zzzzZZZZ = 0; zzzzZZZZ < dataGridView_Productos.Rows.Count; zzzzZZZZ++)
            {
                //Se escribe nodo Concepto
                W.WriteStartElement("cfdi:Concepto");
                //Se escribe atributo Cantidad
                W.WriteStartAttribute("cantidad");
                W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[0].Value);
                W.WriteEndAttribute();
                //Se escribe atributo Unidad
                W.WriteStartAttribute("unidad");
                W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[1].Value);
                W.WriteEndAttribute();
                //Se escribe atributo SKU en caso que exista
                if (dataGridView_Productos.Rows[zzzzZZZZ].Cells[2].Value.ToString() != "")
                {
                    W.WriteStartAttribute("noIdentificacion");
                    W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[2].Value);
                    W.WriteEndAttribute();
                }
                //Se escribe atributo descripcion
                W.WriteStartAttribute("descripcion");
                W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[3].Value);
                W.WriteEndAttribute();
                //Se escribe atributo ValorUnitario
                W.WriteStartAttribute("valorUnitario");
                W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[4].Value);
                W.WriteEndAttribute();
                //Se escribe atributo Importe
                W.WriteStartAttribute("importe");
                W.WriteValue(dataGridView_Productos.Rows[zzzzZZZZ].Cells[5].Value);
                W.WriteEndAttribute();
                //Se cierra el nodo
                W.WriteEndElement();

            }

            W.WriteEndElement(); //Finaliza nodo Conceptos

Lo que yo hago es vaciar de un DataGridView que previamente la persona que esta haciendo el CFDi lleno con sus partidas.

Saluditos.

  #3 (permalink)  
Antiguo 30/01/2014, 13:24
 
Fecha de Ingreso: enero-2014
Mensajes: 14
Antigüedad: 10 años, 3 meses
Puntos: 1
Respuesta: como agregar mas nodos "concepto" en un ciclo for??

hola Namiwis gracias por responder, si no es mucho abusar de tu amabilidad y conocimiento tendras el codigo completo que me lo puedas proporcionar?? o sabes alguna forma de como hacer para agregar los conceptos a la forma que tengo mi código.


Saludos, Gracias...
  #4 (permalink)  
Antiguo 30/01/2014, 15:45
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: como agregar mas nodos "concepto" en un ciclo for??

Buenas,

La forma sería sacar el nodo de conceptos fuera y luego utilizar un bucle (for, foreach, etc):

Código C++:
Ver original
  1. public static void CrearArchivoCFDI()
  2. {
  3.     try
  4.     {
  5.         string selloDigital_Certificado = "resultado";
  6.         string otro = "otro";
  7.         string Resultado = CrearSelloDigitalEmisor(ref selloDigital_Certificado, ref otro);
  8.  
  9.         using (var context = new PL_Datos.DatosBDContext())
  10.         {
  11.             PL_Datos.PLcat_Cotizacion cotizacion = (from co in context.PLcat_Cotizacion
  12.             where co.Id_PLcatCotizacion == 1
  13.             select co).Single();
  14.             foreach (PL_Datos.PLtrn_PartidasCotizacion partidas in cotizacion.PLtrn_PartidasCotizacion)
  15.             {
  16.             }
  17.            
  18.             XElement conceptos = new XElement(cfdi + "Conceptos");         
  19.            
  20.             for(int i = 0; i < 3; i++)
  21.             {
  22.                 conceptos.Add(new XElement(cfdi + "Concepto",
  23.                 new XAttribute("cantidad", ""),
  24.                 new XAttribute("unidad", ""),
  25.                 new XAttribute("noIdentificacion", ""),
  26.                 new XAttribute("descripcion", ""),
  27.                 new XAttribute("valorUnitario", ""),
  28.                 new XAttribute("importe", "")));
  29.             }          
  30.            
  31.             XNamespace cfdi = XNamespace.Get("http://www.sat.gob.mx/cfd/3");
  32.             XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
  33.             XDocument xmlFactura = new XDocument(
  34.             new XDeclaration("1.0", "utf-8", "yes"),
  35.             new XComment("DATOS DE FACTURACION ELECTRONICA"),
  36.             new XElement(cfdi + "Comprobante",
  37.             new XAttribute(XNamespace.Xmlns + "cfdi", "http://www.sat.gob.mx/cfd/3"),
  38.             new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
  39.             new XAttribute(xsi + "schemaLocation", "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd"),
  40.             new XAttribute("version", "3.2"),
  41.             new XAttribute("serie", ""),
  42.             new XAttribute("folio", "00001"),
  43.             new XAttribute("fecha", ""),
  44.             new XAttribute("sello", ""),
  45.             new XAttribute("formaDePago", ""),
  46.             new XAttribute("noCertificado", ""),
  47.             new XAttribute("certificado", ""),
  48.             new XAttribute("condicionesDePago", ""),
  49.             new XAttribute("subTotal", ""),
  50.             new XAttribute("TipoCambio", ""),
  51.             new XAttribute("Moneda", ""),
  52.             new XAttribute("total", ""),
  53.             new XAttribute("metodoDePago", ""),
  54.             new XAttribute("tipoDeComprobante", ""),
  55.             new XAttribute("LugarExpedicion", "Ciudad del Carmen, Campeche, México"),
  56.             new XElement(cfdi + "Emisor",
  57.             new XAttribute("rfc", "GAIL810405RU6"),
  58.             new XAttribute("nombre", "LAZARO GARCIA IZQUIERDO"),
  59.             new XElement(cfdi + "DomicilioFiscal",
  60.             new XAttribute("calle", "40"),
  61.             new XAttribute("noExterior", "85"),
  62.             new XAttribute("colonia", "CUAUHTEMOC CIUDAD DEL CAMREN"),
  63.             new XAttribute("municipio", "CARMEN"),
  64.             new XAttribute("estado", "CAMPECHE"),
  65.             new XAttribute("pais", "MÉXICO"),
  66.             new XAttribute("codigoPostal", "24170")),
  67.             new XElement(cfdi + "RegimenFiscal",
  68.             new XAttribute("Regimen", ""))),
  69.             new XElement(cfdi + "Receptor",
  70.             new XAttribute("rfc", ""),
  71.             new XAttribute("nombre", ""),
  72.             new XElement(cfdi + "Domicilio",
  73.             new XAttribute("pais", ""),
  74.             new XAttribute("calle", ""),
  75.             new XAttribute("estado", ""),
  76.             new XAttribute("colonia", ""),
  77.             new XAttribute("municipio", ""),
  78.             new XAttribute("noExterior", ""),
  79.             new XAttribute("noInterior", ""),
  80.             new XAttribute("codigoPostal", ""))),
  81.             conceptos,
  82.             new XElement(cfdi + "Impuestos",
  83.             new XAttribute("totalImpuestosTrasladados", ""),
  84.             new XElement(cfdi + "Traslados",
  85.             new XElement(cfdi + "Traslado",
  86.             new XAttribute("impuesto", "IVA"),
  87.             new XAttribute("tasa", ""),
  88.             new XAttribute("importe", "")))),
  89.             new XElement(cfdi + "Complemento")));
  90.  
  91.             xmlFactura.Save(@"c:\PartesEnLinea\PL_Presentacion \facturacion\prueba.xml");
  92.         }
  93.     }
  94.     catch (Exception ex)
  95.     {
  96.         throw ex;
  97.     }
  98. }

Ese bucle en un caso real sería un foreach que recorriese una lista de elementos, por ejemplo.

Por supuesto, en el ejemplo que he puesto, estás insertando 3 veces el mismo elemento, lo cual tiene poco sentido.

Hay más maneras de hacerlo, así que si puedes dar un ejemplo más claro de lo que tienes y lo que quieres, podré ampliar más la información.

Un saludo
  #5 (permalink)  
Antiguo 31/01/2014, 17:06
 
Fecha de Ingreso: enero-2014
Mensajes: 14
Antigüedad: 10 años, 3 meses
Puntos: 1
Respuesta: como agregar mas nodos "concepto" en un ciclo for??

Cita:
Iniciado por alexg88 Ver Mensaje
Buenas,

La forma sería sacar el nodo de conceptos fuera y luego utilizar un bucle (for, foreach, etc):

Código C++:
Ver original
  1. public static void CrearArchivoCFDI()
  2. {
  3.     try
  4.     {
  5.         string selloDigital_Certificado = "resultado";
  6.         string otro = "otro";
  7.         string Resultado = CrearSelloDigitalEmisor(ref selloDigital_Certificado, ref otro);
  8.  
  9.         using (var context = new PL_Datos.DatosBDContext())
  10.         {
  11.             PL_Datos.PLcat_Cotizacion cotizacion = (from co in context.PLcat_Cotizacion
  12.             where co.Id_PLcatCotizacion == 1
  13.             select co).Single();
  14.             foreach (PL_Datos.PLtrn_PartidasCotizacion partidas in cotizacion.PLtrn_PartidasCotizacion)
  15.             {
  16.             }
  17.            
  18.             XElement conceptos = new XElement(cfdi + "Conceptos");         
  19.            
  20.             for(int i = 0; i < 3; i++)
  21.             {
  22.                 conceptos.Add(new XElement(cfdi + "Concepto",
  23.                 new XAttribute("cantidad", ""),
  24.                 new XAttribute("unidad", ""),
  25.                 new XAttribute("noIdentificacion", ""),
  26.                 new XAttribute("descripcion", ""),
  27.                 new XAttribute("valorUnitario", ""),
  28.                 new XAttribute("importe", "")));
  29.             }          
  30.            
  31.             XNamespace cfdi = XNamespace.Get("http://www.sat.gob.mx/cfd/3");
  32.             XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
  33.             XDocument xmlFactura = new XDocument(
  34.             new XDeclaration("1.0", "utf-8", "yes"),
  35.             new XComment("DATOS DE FACTURACION ELECTRONICA"),
  36.             new XElement(cfdi + "Comprobante",
  37.             new XAttribute(XNamespace.Xmlns + "cfdi", "http://www.sat.gob.mx/cfd/3"),
  38.             new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
  39.             new XAttribute(xsi + "schemaLocation", "http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd"),
  40.             new XAttribute("version", "3.2"),
  41.             new XAttribute("serie", ""),
  42.             new XAttribute("folio", "00001"),
  43.             new XAttribute("fecha", ""),
  44.             new XAttribute("sello", ""),
  45.             new XAttribute("formaDePago", ""),
  46.             new XAttribute("noCertificado", ""),
  47.             new XAttribute("certificado", ""),
  48.             new XAttribute("condicionesDePago", ""),
  49.             new XAttribute("subTotal", ""),
  50.             new XAttribute("TipoCambio", ""),
  51.             new XAttribute("Moneda", ""),
  52.             new XAttribute("total", ""),
  53.             new XAttribute("metodoDePago", ""),
  54.             new XAttribute("tipoDeComprobante", ""),
  55.             new XAttribute("LugarExpedicion", "Ciudad del Carmen, Campeche, México"),
  56.             new XElement(cfdi + "Emisor",
  57.             new XAttribute("rfc", "GAIL810405RU6"),
  58.             new XAttribute("nombre", "LAZARO GARCIA IZQUIERDO"),
  59.             new XElement(cfdi + "DomicilioFiscal",
  60.             new XAttribute("calle", "40"),
  61.             new XAttribute("noExterior", "85"),
  62.             new XAttribute("colonia", "CUAUHTEMOC CIUDAD DEL CAMREN"),
  63.             new XAttribute("municipio", "CARMEN"),
  64.             new XAttribute("estado", "CAMPECHE"),
  65.             new XAttribute("pais", "MÉXICO"),
  66.             new XAttribute("codigoPostal", "24170")),
  67.             new XElement(cfdi + "RegimenFiscal",
  68.             new XAttribute("Regimen", ""))),
  69.             new XElement(cfdi + "Receptor",
  70.             new XAttribute("rfc", ""),
  71.             new XAttribute("nombre", ""),
  72.             new XElement(cfdi + "Domicilio",
  73.             new XAttribute("pais", ""),
  74.             new XAttribute("calle", ""),
  75.             new XAttribute("estado", ""),
  76.             new XAttribute("colonia", ""),
  77.             new XAttribute("municipio", ""),
  78.             new XAttribute("noExterior", ""),
  79.             new XAttribute("noInterior", ""),
  80.             new XAttribute("codigoPostal", ""))),
  81.             conceptos,
  82.             new XElement(cfdi + "Impuestos",
  83.             new XAttribute("totalImpuestosTrasladados", ""),
  84.             new XElement(cfdi + "Traslados",
  85.             new XElement(cfdi + "Traslado",
  86.             new XAttribute("impuesto", "IVA"),
  87.             new XAttribute("tasa", ""),
  88.             new XAttribute("importe", "")))),
  89.             new XElement(cfdi + "Complemento")));
  90.  
  91.             xmlFactura.Save(@"c:\PartesEnLinea\PL_Presentacion \facturacion\prueba.xml");
  92.         }
  93.     }
  94.     catch (Exception ex)
  95.     {
  96.         throw ex;
  97.     }
  98. }

Ese bucle en un caso real sería un foreach que recorriese una lista de elementos, por ejemplo.

Por supuesto, en el ejemplo que he puesto, estás insertando 3 veces el mismo elemento, lo cual tiene poco sentido.

Hay más maneras de hacerlo, así que si puedes dar un ejemplo más claro de lo que tienes y lo que quieres, podré ampliar más la información.

Un saludo
Gracias por responder voy a probar tu código y te comento como me fue, si claro va ser el mismo elemento solo cambiara el valor de sus tributos, es por decir el usuario guarda 6 partidas entonces serian 6 elementos con sus atributos pero valores diferentes
XElement conceptos = new XElement(cfdi + "Conceptos");

for(int i = 0; i < 3; i++)
{
conceptos.Add(new XElement(cfdi + "Concepto",
new XAttribute("cantidad", ""),
new XAttribute("unidad", ""),
new XAttribute("noIdentificacion", ""),
new XAttribute("descripcion", ""),
new XAttribute("valorUnitario", ""),
new XAttribute("importe", "")));
}
probare este código a ver que tal me va y te comento
  #6 (permalink)  
Antiguo 01/02/2014, 12:52
 
Fecha de Ingreso: enero-2014
Mensajes: 14
Antigüedad: 10 años, 3 meses
Puntos: 1
Respuesta: como agregar mas nodos "concepto" en un ciclo for??

Gracias por responder a este tema alexg88 y Namiwis ya que con sus aportes pude resolver este tema de agregar mas conceptos al xml para la facturacion electronica.

alexg88 tu código me funciono de maravilla gracias por tu aporte compañero.

saludos y nuevamente gracias por responder.

Etiquetas: concepto, net, nodos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:27.