在vb.net中使用多个子标记进行反序列化

时间:2014-04-30 14:33:15

标签: xml vb.net serialization datagridview

大家好我是新来的我有一个invoiceList xml文件,我需要反序列化我不能控制元素来改变它们,因为我将从外部源获取它。

样本发票就是这样

<InvoicesList type="Invoices" date="17-11-2011" customerid="999999" version="1.0">

<invoice>
<number>9123123123</number>
<custid>999999</custid>
<custname>Your Company Name</custname>
<date>2011-11-15T00:00:00</date>
<vessel></vessel>
<saildate>2011-11-15T00:00:00</saildate>
<manifestref>HD56448/61</manifestref>
<reference>00</reference>
<customerref>2331748</customerref>

</invoice>

<invoice>
<number>9259854525</number>
<custid>999999</custid>
<custname>Your Company Name</custname>
<date>2011-11-15T00:00:00</date>
<manifestref>HD56448/61</manifestref>
<reference>00</reference>
<customerref>2331748</customerref>
<weight>0000000000</weight>
<length>0000000013</length>
<paymentdue>452.65</paymentdue>
<icgvatno>IE 123123123J</icgvatno>
<custvatno>IE 321321321J</custvatno>
</invoice>
</InvoicesList> 

我有一个InvoicesList类,它只具有get和set的发票属性 那么我有发票类,它具有所需的所有属性,

现在我可以将第一张发票反序列化没有问题,并将其显示在Windows窗体应用程序的数据网格视图中,但似乎无法让另一张进入其中,我的反序列化代码如下

Dim objStreamReader As New StreamReader(filepath) 'Path where file is
        Dim x As New XmlSerializer(GetType(InvoicesList))
        O = x.Deserialize(objStreamReader)  'O is reference as object of invoicesList
        objStreamReader.Close()
        Dim Invoices As New List(Of invoice)
        Invoices.Add(CType(O, InvoicesList).invoice)
        Form1.DataGridView1.DataSource = Invoices

我想要的是第二张发票进入datagridview的下一行,invoicesList中可能有多张发票,所以我知道for循环最好, 希望你能帮忙。如果需要其他任何东西请告诉我。 我知道两者中的元素不同,我只是为了节省空间而剪掉了很多元素。

这是我的发票类我再次删除了很多属性以节省空间

Public Class invoice
Private numberProperty As String
Private custidProperty As String
Private custnameProperty As String
Private routeProperty As String
Private manifestrefProperty As String
Private referenceProperty As String
Private customerrefProperty As String
Private weightProperty As String
Private lengthProperty As String
Private vehicleregProperty As String
Private unitProperty As String
Private unittypeProperty As String
Private goodsProperty As String
Private paymentdueProperty As Decimal
Private icgvatnoProperty As String
Private custvatnoProperty As String


Public Property number As String
    Get
        Return numberProperty
    End Get
    Set(value As String)
        numberProperty = value
    End Set
End Property

Public Property custid As String
    Get
        Return custidProperty
    End Get
    Set(value As String)
        custidProperty = value
    End Set
End Property

Public Property custname As String
    Get
        Return custnameProperty
    End Get
    Set(value As String)
        custnameProperty = value
    End Set
End Property

Public Property unittype As String
    Get
        Return unittypeProperty
    End Get
    Set(value As String)
        unittypeProperty = value
    End Set
End Property

Public Property goods As String
    Get
        Return goodsProperty
    End Get
    Set(value As String)
        goodsProperty = value
    End Set
End Property

Public Property sparedesc2 As String
    Get
        Return sparedesc2Property
    End Get
    Set(value As String)
        sparedesc2Property = value
    End Set
End Property

Public Property sparedesc3 As String
    Get
        Return sparedesc3Property
    End Get
    Set(value As String)
        sparedesc3Property = value
    End Set
End Property

Public Property sparedesc4 As String
    Get
        Return sparedesc4Property
    End Get
    Set(value As String)
        sparedesc4Property = value
    End Set
End Property

Public Property channelbookingamount As Decimal
    Get
        Return ChannelbookingamountProperty
    End Get
    Set(value As Decimal)
        ChannelbookingamountProperty = value
    End Set
End Property

Public Property vatamount As Decimal
    Get
        Return vatamountProperty
    End Get
    Set(value As Decimal)
        vatamountProperty = value
    End Set
End Property

Public Property spareamount5 As Decimal
    Get
        Return spareamount5Property
    End Get
    Set(value As Decimal)
        spareamount5Property = value
    End Set
End Property

Public Property driver1 As String
    Get
        Return driver1Property
    End Get
    Set(value As String)
        driver1Property = value
    End Set
End Property

Public Property driver2 As String
    Get
        Return driver2Property
    End Get
    Set(value As String)
        driver2Property = value
    End Set
End Property

Public Property driver3 As String
    Get
        Return driver3Property
    End Get
    Set(value As String)
        driver3Property = value
    End Set
End Property

Public Property driver4 As String
    Get
        Return driver4Property
    End Get
    Set(value As String)
        driver4Property = value
    End Set
End Property

Public Property driver5 As String
    Get
        Return driver5Property
    End Get
    Set(value As String)
        driver5Property = value
    End Set
End Property

Public Property abnormalload As String
    Get
        Return abnormalloadProperty
    End Get
    Set(value As String)
        abnormalloadProperty = value
    End Set
End Property

Public Property paymentdue As Decimal
    Get
        Return paymentdueProperty
    End Get
    Set(value As Decimal)
        paymentdueProperty = value
    End Set
End Property

Public Property icgvatno As String
    Get
        Return icgvatnoProperty
    End Get
    Set(value As String)
        icgvatnoProperty = value
    End Set
End Property

Public Property custvatno As String
    Get
        Return custvatnoProperty
    End Get
    Set(value As String)
        custvatnoProperty = value
    End Set
End Property
End class

然后我有我的invoicesList类

Public Class InvoicesList
Private InvoiceProperty As invoice
Public Property invoice As invoice
    Get
        Return InvoiceProperty
    End Get
    Set(value As invoice)
        InvoiceProperty = value
    End Set
End Property
End class

1 个答案:

答案 0 :(得分:1)

您只在列表中存储一张发票(请参阅您的.NET类),这就是为什么它只能读取一个项目。将其更改为从通用列表继承是一种方法(我认为最容易解释)。

使用您的XML而不做任何修改,请将您的InvoicesList课程更改为以下内容:

Public Class InvoicesList : Inherits List(Of invoice)
End Class

然后你的反序列化过程:

Dim O As InvoicesList
Using objStreamReader As New StreamReader(filePath)
  Dim x As New XmlSerializer(GetType(InvoicesList), New XmlRootAttribute("InvoicesList"))
  O = x.Deserialize(objStreamReader)
End Using

注意这里的技巧 - XmlSerializer的第二个参数,the approach is described here

您不需要在类中添加可序列化属性,我原本期望这是反序列化工作的要求。剩下的应该是好的。