简体   繁体   中英

How to read inner data in XML file using Deserializer?

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:

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

Then you could deserialize it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM