This is my XML file:
<?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>
And this is my code:
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
And my classes:
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
It works, it gives me the Message Boxes with the data in the XML file, I’m having trouble however, if I add inner nodes to the XML file like this:
<?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>
How am I supposed to work numbers out? I know it’s a property of test, but it’s also a class because it has one and two as properties, so what would the right approach be?
Update:
<?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>
To deserialize that example XML, you’d want your data classes to look like this:
Then you could deserialize it like this: