Problem when creating XML document from a class

1

I develop an application where I create an XML document from a class which I serialize, the problem occurs when a certain node must be repeated more than once, I explain the document that I am creating is an invoice if I sell an article the node appears an Once, if I sell two items the node will appear twice and so on.

How can I create this node as many times as the number of products my invoice has?

The code I use to add the node is the following, how can I modify it so that it repeats as many times as necessary?

 DetalleServicio = new FacturaElectronicaLineaDetalle[]
                  {
                      new FacturaElectronicaLineaDetalle
                   {
                      NumeroLinea = numeroLinea,
                      Codigo = new CodigoType[]
                      {
                          new CodigoType
                          {
                            Tipo=(CodigoTypeTipo)Enum.Parse(typeof(CodigoTypeTipo), tipo),
                            Codigo= codigo
                          }
                      },
                      Cantidad = cantidad,
                      UnidadMedida=(UnidadMedidaType)Enum.Parse(typeof(UnidadMedidaType), unidadMedida),
                      UnidadMedidaComercial=unidadComercial,
                      Detalle= detalle,
                      PrecioUnitario=precio,
                      MontoTotal=precio * cantidad,
                      MontoDescuento=montoDescuento,
                      NaturalezaDescuento= naturalezaDescuento,
                      SubTotal= subTotal,
                      MontoTotalLinea= precio * cantidad - montoDescuento
                  }
               }
    
asked by Efrain Mejias C 25.02.2018 в 20:17
source

1 answer

0

I built this method that returns a list, according to what I saw here: Problem when creating XML document from a class

  public List<FacturaElectronicaLineaDetalle> DetalleFactura(List<TributarioCr.ClaseUtil.LineaDetalle> detalleFactura)
        {
            List<FacturaElectronicaLineaDetalle> detalleServicio = new List<FacturaElectronicaLineaDetalle>();

            for (int i = 0; i <= detalleFactura.Count - 1; i++)
            {
                FacturaElectronicaLineaDetalle linea = new FacturaElectronicaLineaDetalle
                {
                    NumeroLinea = (i + 1).ToString (),
                    Codigo = new CodigoType[]
                      {
                          new CodigoType
                          {
                            Tipo=(CodigoTypeTipo)Enum.Parse(typeof(CodigoTypeTipo), detalleFactura[i].tipo),
                            Codigo=detalleFactura[i].codigo
                          }
                      },
                    Cantidad = Convert.ToDecimal(detalleFactura[i].cantidad),
                    UnidadMedida = (UnidadMedidaType)Enum.Parse(typeof(UnidadMedidaType), detalleFactura[i].unidadMedida),
                    UnidadMedidaComercial = detalleFactura[i].unidadMedidaComercial,
                    Detalle = detalleFactura[i].detalleServicio,
                    PrecioUnitario = Convert.ToDecimal(detalleFactura[i].precioUnitario),
                    MontoTotal = Convert.ToDecimal(detalleFactura[i].montoTotal),
                    MontoDescuento = Convert.ToDecimal(detalleFactura[i].montoDescuento),
                    // MontoDescuentoSpecified=true,
                    NaturalezaDescuento = detalleFactura[i].naturalezaDescuento,
                    SubTotal = Convert.ToDecimal(detalleFactura[i].subTotal),

                    MontoTotalLinea = Convert.ToDecimal(detalleFactura[i].montoTotalLinea)
                };

                detalleServicio.Add(linea);
            }

            return detalleServicio;
        }
    
answered by 26.02.2018 в 16:49