2012-10-08 35 views
0

這是我的XML文件:如何使用Deserializer讀取XML文件中的內部數據?

<?xml version="1.0" encoding="utf-8"?> 
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<name>test</name> 
<one>1</one> 
<two>2</two> 
</test> 

這是我的代碼:

Imports System.Xml.Serialization 
Imports System.IO 

Class main 

Sub Main() 
    Dim p As New test() 

    Dim x As New XmlSerializer(p.GetType) 

    Dim objStreamReader As New StreamReader("XML.xml") 
    Dim p2 As New class1() 
    p2 = x.Deserialize(objStreamReader) 
    objStreamReader.Close() 

    MsgBox(p2.name) 
    MsgBox(p2.one) 
    MsgBox(p2.two) 

End Sub 

End Class 

而且我的課:

Imports System.Xml.Serialization 

Public Class test 

Private newname As String 
Private newone As Integer 
Private newtwo As Integer 

Public Property name() As String 
    Get 
     name = newname 
    End Get 
    Set(ByVal value As String) 
     newname= value 
    End Set 
End Property 

Public Property one() As Integer 
    Get 
     one = newone 
    End Get 
    Set(ByVal value As Integer) 
     newone = value 
    End Set 
End Property 

Public Property two() As Integer 
    Get 
     two = newtwo 
    End Get 
    Set(ByVal value As Integer) 
     newtwo = value 
    End Set 
End Property 

End Class 

它的工作原理,它給我的消息框與XML文件中的數據,但是我遇到了麻煩,但是如果我將內部節點添加到XML文件中,如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<name>test</name> 
<numbers> 
    <one>1</one> 
    <two>2</two> 
</numbers> 
</test> 

我該如何處理數字?我知道這是一個測試屬性,但它也是一個類,因爲它有一個和兩個屬性,所以正確的方法是什麼?

更新:

<?xml version="1.0" encoding="utf-8"?> 
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<name>test</name> 
<numbers> 
    <one>1</one> 
    <two>2</two> 
</numbers> 
<numbers> 
    <one>3</one> 
    <two>4</two> 
</numbers> 
</test> 

回答

1

反序列化示例XML,你會希望自己的數據類,看起來像這樣:

Public Class test 
    Private newname As String 
    Private newnumbers As List(Of Numbers) 

    Public Property name() As String 
     Get 
      Return newname 
     End Get 
     Set(ByVal value As String) 
      newname = value 
     End Set 
    End Property 

    <XmlElement()> _ 
    Public Property numbers() As List(Of Numbers) 
     Get 
      Return newnumbers 
     End Get 
     Set(ByVal value As List(Of Numbers)) 
      newnumbers = value 
     End Set 
    End Property 
End Class 

Public Class Numbers 
    Private newone As Integer 
    Private newtwo As Integer 

    Public Property one() As Integer 
     Get 
      Return newone 
     End Get 
     Set(ByVal value As Integer) 
      newone = value 
     End Set 
    End Property 

    Public Property two() As Integer 
     Get 
      Return newtwo 
     End Get 
     Set(ByVal value As Integer) 
      newtwo = value 
     End Set 
    End Property 
End Class 

然後,你可以反序列化這樣的:

Sub Main() 
    Dim p As New test() 
    Dim x As New XmlSerializer(p.GetType) 
    Dim objStreamReader As New StreamReader("XML.xml") 
    Dim p2 As New test() 
    p2 = x.Deserialize(objStreamReader) 
    objStreamReader.Close() 
    MsgBox(p2.name) 
    For Each i As Numbers In p2.numbers 
     MsgBox(i.one) 
     MsgBox(i.two) 
    Next 
End Sub 
+0

我會打電話給班級考試的屬性嗎?或者只是類數字的屬性? – user1676874

+0

它給了我這個錯誤:當我調用Deserialize方法將其分配給其他類時,XML文檔(0,0)中存在錯誤:o2 = x.Deserialize(objStreamReader),是否可能是因爲它已經被反序列化? – user1676874

+0

@ user1676874您只需將其反序列化爲單個'test'對象並通過該對象訪問numbers屬性即可。我更新了我的答案以提供示例。 –