' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Collections.Generic
Imports System.Xml
Imports System.Xml.Serialization

Namespace E
    Public Class typex
    End Class
    Public Class type1
        Inherits typex
        Public Property tekst() As String
    End Class
    Public Class type2
        Inherits typex
        Public Property tekst() As String
    End Class
    Public Class type3
        Inherits typex
        Public Property tekst() As String
    End Class
    Public Class xmltest
        <XmlElement(Type := GetType(type1)), XmlElement(Type := GetType(type2)), XmlElement(Type := GetType(type3))> _
        Public Property typex() As List(Of typex)
    End Class
    Public Class Program
        Public Shared Sub Dump(o As xmltest)
            For Each tx As typex In o.typex
                Select Case tx.[GetType]().Name
                    Case "type1"
                        Dim t1 As type1 = DirectCast(tx, type1)
                        Console.WriteLine("Type1 : {0}", t1.tekst)
                        Exit Select
                    Case "type2"
                        Dim t2 As type2 = DirectCast(tx, type2)
                        Console.WriteLine("Type2 : {0}", t2.tekst)
                        Exit Select
                    Case "type3"
                        Dim t3 As type3 = DirectCast(tx, type3)
                        Console.WriteLine("Type3 : {0}", t3.tekst)
                        Exit Select
                End Select
            Next
        End Sub
        Public Shared Sub Main(args As String())
            Dim ser As New XmlSerializer(GetType(xmltest), New Type() {GetType(type1), GetType(type2), GetType(type3)})
            Dim rdr As XmlReader = New XmlTextReader("/Work/test.xml")
            While rdr.Read()
                Select Case rdr.NodeType
                    Case XmlNodeType.Element
                        Dim subrdr As XmlReader = rdr.ReadSubtree()
                        Dim o As xmltest = DirectCast(ser.Deserialize(subrdr), xmltest)
                        Dump(o)
                        subrdr.Close()
                        Exit Select
                    Case Else
                        ' ignore
                        Exit Select
                End Select
            End While
            rdr.Close()
        End Sub
    End Class
End Namespace
