2012-10-16 3 views
1

여러 개의 목록이 있다는 사실 때문에이 테이블을 비 직렬화하는 데 문제가 있습니다. 반복 할 때 목록에 내 목록이 필요하다는 것을 이해합니다. 또한 반복 할 때 내 tds에도 목록이 필요합니다. 문제는 tds의 값을 읽으 려 할 때 나온다. 목록 형식으로되어 있기 때문이다.여러 목록을 가진 테이블을 deserialize하는 방법

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

그리고 내 수업 : 여기

내 XML의

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 안에? 감사!

답변

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 
+1

다시 스티븐이 아닌 경우. :) – user1676874

+0

아! 그것은 당신입니다 :) 나는 당신의 숫자로 당신을 인식하지 못했습니다. 내 사진을 업데이트해야합니다. 약간 불안해합니다. –

+1

나는 그것을 고쳐야하고, 프로그래머처럼 보일 뿐이다. :) – user1676874

관련 문제