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