【问题标题】:Issue with Object to XML serialization in VB.NetVB.Net 中对象到 XML 序列化的问题
【发布时间】:2018-08-25 21:15:45
【问题描述】:

我在使用 XSD 架构和 XML 的电子发票系统中工作,我有来自 XSD 的 vb 类,我正在尝试序列化 XML,但我似乎可以制作一个复杂的类型工作。 所以我有一个 Invoice 类,里面有一个细节类,细节里面有一个 CodeType 类。 对于有几行的发票,比如说 4,我应该在 CodoType 字段中有 4 个不同的代码,但是每个新代码都会覆盖最后一个,所以最后我有 4 个代码字段具有完全相同的数据,代码对于第 4 项。 如果我尝试将其更改为所有 4 更改为相同。 这是类架构: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 方面没有太多经验,所以很抱歉。

最好的问候。

【问题讨论】:

    标签: xml vb.net serialization complextype


    【解决方案1】:

    问题在于,即使您在ProductoCodigos0 数组中设置了一个新项目,该数组本身仍然保持不变。这意味着您的所有类实例当前都引用完全相同的数组。

    在循环外声明一次性变量通常不是很好的做法。如果您在循环中声明了所有这些(items 除外),那么您将永远不会遇到这个问题。

    最后,不要将items 声明为固定大小的数组,而是使用动态大小的List(Of T) 来更好地避免NullReferenceExceptions 和IndexOutOfRangeExceptions 当您有超过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()
    

    【讨论】:

    • 你修复了文森特!!!非常感谢您的帮助。你真棒。
    • @SteelMora :很高兴我能帮上忙!请按我帖子左侧的勾选/复选标记,将我的回答标记为已接受。更多信息请参考:How does accepting an answer work?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多