2012-10-08 33 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属性即可。我更新了我的答案以提供示例。 –