简体   繁体   中英

Issue with Object to XML serialization in VB.Net

I'm working in a Electronic Invoice system that works with XSD Schemas and XML, I have my vb classes from the XSD and I'm trying to serialize the XML but there is a complex type that I can seem to make it work. So I have an Invoice class, with a detail class within and a CodeType class within the detail. For an invoice with several lines, let's say 4, I'm supposed to have 4 different codes in the field CodoType, however each new code overwrites the last one so at the end I have 4 code fields with the exact same data, the code for the 4th item. If I try to change it all 4 change to the same. Here' the Class Schema : Classes

Here's my code:

  enter code here 
Dim Producto As New FacturaElectronicaLineaDetalle
Dim items(20) As FacturaElectronicaLineaDetalle
Dim Codigo0 As New CodigoType
Dim ProductoCodigos0(0) As CodigoType

For Each row As DataGridViewRow In grdDetalle.Rows
            If row.Cells(0).Value = Nothing Then Exit For

            Producto.NumeroLinea = row.Index + 1
            Producto.Cantidad = row.Cells(4).Value
            Producto.UnidadMedida = 1
            Producto.Detalle = row.Cells(2).Value
            Producto.PrecioUnitario = FormatNumber(row.Cells(3).Value, 5)
            Producto.MontoTotal = Producto.PrecioUnitario * Producto.Cantidad
            Producto.MontoDescuento = 0.00000
            Producto.SubTotal = Producto.MontoTotal - Producto.MontoDescuento

            Codigo0 = New CodigoType
            Codigo0.Tipo = CodigoTypeTipo.Item01
            Codigo0.Codigo = row.Cells(1).Value
            ProductoCodigos0(0) = New CodigoType
            ProductoCodigos0(0) = Codigo0
            Producto.Codigo = ProductoCodigos0
            items(row.Index) = Producto
            Producto = New FacturaElectronicaLineaDetalle

        Next row

        Factura.DetalleServicio = items

Thanks in advance for your help, I don't have much experience in XML so I apologize.

Best Regards.

The problem is that even though you set a new item in the ProductoCodigos0 array, the array itself remains the same. Meaning all of your class instances currently refer to the exact same array.

It is generally not very good practice to declare one-time variables outside a loop. If you would have declared all of them (except for items ) inside the loop instead, you would never have had this problem.

Finally, instead of declaring items as a fixed size array, use a dynamic sized List(Of T) to better avoid NullReferenceException s, and IndexOutOfRangeException s when you have more than 21 items.

Dim items As New List(Of FacturaElectronicaLineaDetalle)

For Each row As DataGridViewRow In grdDetalle.Rows
    If row.Cells(0).Value = Nothing Then Exit For

    Dim Producto As New FacturaElectronicaLineaDetalle
    Producto.NumeroLinea = row.Index + 1
    Producto.Cantidad = row.Cells(4).Value
    Producto.UnidadMedida = 1
    Producto.Detalle = row.Cells(2).Value
    Producto.PrecioUnitario = FormatNumber(row.Cells(3).Value, 5)
    Producto.MontoTotal = Producto.PrecioUnitario * Producto.Cantidad
    Producto.MontoDescuento = 0.00000
    Producto.SubTotal = Producto.MontoTotal - Producto.MontoDescuento

    Dim Codigo As New CodigoType
    Codigo.Tipo = CodigoTypeTipo.Item01
    Codigo.Codigo = row.Cells(1).Value
    Producto.Codigo = New CodigoType() {Codigo} 'Here we create the CodigoType array instead.

    items.Add(Producto)
Next

Factura.DetalleServicio = items.ToArray()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM