【问题标题】:How to deserialize table with multiple lists如何反序列化具有多个列表的表
【发布时间】:2012-10-16 04:41:45
【问题描述】:

由于有多个列表,我在反序列化此表时遇到了麻烦,我知道我需要一个列表,因为我的 trs 重复,而且我的 tds 也重复,当尝试阅读时出现问题tds 的值,因为我有它的列表格式。

这是我的 xml:

<table>
 <tr>
  <td>1</td>
  <td>2</td>
 </tr>
 <tr>
  <td>3</td>
  <td>4</td>
 </tr>
</table> 

还有我的课:

Public Class table
    Private newtr As List(Of tr)
    <XmlElement()> _
    Public Property tr() As List(Of tr)
        Get
            Return newtr
        End Get
        Set(ByVal value As List(Of tr))
            newtr = value
        End Set
    End Property
End Class


Public Class tr
    Private newtd As List(Of td)
    <XmlElement()> _
    Public Property td() As List(Of td)
        Get
            Return newtd
        End Get
        Set(ByVal value As List(Of td))
            newtd = value
        End Set
    End Property
End Class


Public Class td
    Private newvalue As String
    <XmlElement()> _
    Public Property td() As String
        Get
            Return newvalue
        End Get
        Set(ByVal value As String)
            newvalue = value
        End Set
    End Property
End Class

还有我的代码:

Public Sub test2()
    Dim rr As New table()
    Dim xx As New XmlSerializer(rr.GetType)
    Dim objStreamReader2 As New StreamReader("table.xml")
    Dim rr2 As New table()
    rr2 = xx.Deserialize(objStreamReader2)
    For Each ii As tr In rr2.tr
        MsgBox(ii.td)
    Next
End Sub

那么关于如何获取 tds 中的每个值的任何想法?谢谢!

【问题讨论】:

    标签: html vb.net xml-serialization


    【解决方案1】:

    您当前已将tr.td 声明为列表,因此您不能仅将其输出为单个字符串。您需要遍历列表中的每个 td 项目:

    For Each currTr As tr In rr2.tr
        For Each currTd As td In currTr.td
            MessageBox.Show(currTd.td)
        Next
    Next
    

    但是,这不会正确读取示例 XML 中的值。在您的示例中,每个 td 元素都包含一个字符串,而不是另一个同名的子元素。但是您的数据结构假定 XML 的结构如下所示:

    <table>
     <tr>
      <td>
       <td>1</td>
      </td>
      <td>
       <td>2</td>
      </td>
     </tr>
     <tr>
      <td>
       <td>3</td>
      </td>
      <td>
       <td>4</td>
      </td>
     </tr>
    </table>
    

    要解决这个问题,您只需要像这样的两个类:

    Public Class table
        Private newtr As List(Of tr)
        <XmlElement()> _
        Public Property tr() As List(Of tr)
            Get
                Return newtr
            End Get
            Set(ByVal value As List(Of tr))
                newtr = value
            End Set
        End Property
    End Class
    
    
    Public Class tr
        Private newtd As List(Of String)
        <XmlElement()> _
        Public Property td() As List(Of String)
            Get
                Return newtd
            End Get
            Set(ByVal value As List(Of String))
                newtd = value
            End Set
        End Property
    End Class
    

    然后,您可以像这样遍历反序列化的对象:

    For Each currTr As tr In rr2.tr
        For Each currTd As String In currTr.td
            MessageBox.Show(currTd)
        Next
    Next
    

    【讨论】:

    • 啊!是你 :) 我没有通过你的数值认出你。我需要更新我的照片。恐怕有点太令人不安了。
    • 我应该解决这个问题,而且你看起来像个程序员,这没什么错。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2021-11-08
    相关资源
    最近更新 更多