将具有多个表的XML反序列化为列表

时间:2019-07-11 07:40:36

标签: xml vb.net list deserialization

我在互联网上找到了以下代码。我有以下XML(添加了公司),但原始xml仅包含Quote表。

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfQuote xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<Quote>
    <QuoteText>Hello World</QuoteText>
    <Author>Unknown</Author>
    <Category>Some Category</Category>
    <QuoteDate>0001-01-01T00:00:00</QuoteDate>
</Quote>

<Quote>
    <QuoteText>Foo Bar</QuoteText>
    <Author>Somebody Else</Author>
    <Category>Some Other Category</Category>
    <QuoteDate>9999-12-31T23:59:59.9999999</QuoteDate>
</Quote>

<Company>
    <Name>Name1</Name>
    <City>Unknown</City>
    <Zip>34354</Zip>
</Company>

<Company>
    <Name>Name2</Name>
    <City>Unknown</City>
    <Zip>5656</Zip>
</Company>

</ArrayOfQuote>

我在vb.net中有以下代码

Public _Quote As List(Of Quote)
Public _Company As List(Of Company)

Sub Deserialize(filePath As String) As List(Of Quote)    

    Using sr As New StreamReader(filePath)
        Dim serializer As New XmlSerializer(GetType(List(Of Quote)))
       _Quote = CType(serializer.Deserialize(sr), List(Of Quote))
    End Using

End Sub

<Serializable()>
Public Class Quote
    Public Property QuoteText As String
    Public Property Author As String
    Public Property Category As String
    Public Property QuoteDate As DateTime
End Class

<Serializable()>
Public Class Company
    Public Property Name As String
    Public Property City As String
    Public Property Zip As Integer
End Class

上面的sub将我的XML转换为List(Of Quote),但我想修改该sub,以便它还生成List(Of Company)以及其他表格。我将在一个目录中使用单独的XML,并希望为XML中的每个表生成列表。有什么帮助吗?谢谢。

0 个答案:

没有答案