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>
我會打電話給班級考試的屬性嗎?或者只是類數字的屬性? – user1676874
它給了我這個錯誤:當我調用Deserialize方法將其分配給其他類時,XML文檔(0,0)中存在錯誤:o2 = x.Deserialize(objStreamReader),是否可能是因爲它已經被反序列化? – user1676874
@ user1676874您只需將其反序列化爲單個'test'對象並通過該對象訪問numbers屬性即可。我更新了我的答案以提供示例。 –