VB.Net 中的对象 XML 序列化问题
Issue with Object to XML serialization in VB.Net
我在使用 XSD 架构和 XML 的电子发票系统中工作,我的 vb classes 来自 XSD 并且我正在尝试序列化 XML 但是有一个复杂的类型我似乎可以让它工作。
因此,我有一张发票 class,其中包含详细信息 class 和详细信息中的代码类型 class。
对于有几行的发票,比如说 4,我应该在字段 CodoType 中有 4 个不同的代码,但是每个新代码都会覆盖最后一个代码,所以最后我有 4 个代码字段具有完全相同的数据,代码对于第 4 项。
如果我尝试更改它,所有 4 个更改为相同。
这是 Class 架构:Classes
这是我的代码:
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
在此先感谢您的帮助,我在这方面的经验不多XML,所以很抱歉。
此致。
问题在于,即使您在 ProductoCodigos0
数组中设置了新项目,数组本身仍然保持不变。这意味着您的所有 class 个实例当前都引用完全相同的数组。
在循环外声明一次性变量通常不是很好的做法。如果您在循环内声明所有这些(items
除外),您将永远不会遇到这个问题。
最后,不要将 items
声明为固定大小的数组,而是使用动态大小的 List(Of T)
来更好地避免 NullReferenceException
s 和 IndexOutOfRangeException
s 当你有超过 21 项。
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()
我在使用 XSD 架构和 XML 的电子发票系统中工作,我的 vb classes 来自 XSD 并且我正在尝试序列化 XML 但是有一个复杂的类型我似乎可以让它工作。 因此,我有一张发票 class,其中包含详细信息 class 和详细信息中的代码类型 class。 对于有几行的发票,比如说 4,我应该在字段 CodoType 中有 4 个不同的代码,但是每个新代码都会覆盖最后一个代码,所以最后我有 4 个代码字段具有完全相同的数据,代码对于第 4 项。 如果我尝试更改它,所有 4 个更改为相同。 这是 Class 架构:Classes
这是我的代码:
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
在此先感谢您的帮助,我在这方面的经验不多XML,所以很抱歉。
此致。
问题在于,即使您在 ProductoCodigos0
数组中设置了新项目,数组本身仍然保持不变。这意味着您的所有 class 个实例当前都引用完全相同的数组。
在循环外声明一次性变量通常不是很好的做法。如果您在循环内声明所有这些(items
除外),您将永远不会遇到这个问题。
最后,不要将 items
声明为固定大小的数组,而是使用动态大小的 List(Of T)
来更好地避免 NullReferenceException
s 和 IndexOutOfRangeException
s 当你有超过 21 项。
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()