' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.IO
Imports System.Text
Imports System.Xml

Namespace XmlProc.Pull
    Public Class Program
        Private Const XML_FILE As String = "C:\work\employees.xml"
        Public Shared Sub Main(args As String())
            ' open file
            Using sr As New StreamReader(XML_FILE)
                Dim xr As XmlReader = New XmlTextReader(sr)
                ' process file
                Dim sb As New StringBuilder()
                While xr.Read()
                    Select Case xr.NodeType
                        ' accumulate text
                        Case XmlNodeType.Text
                            sb.Append(xr.Value)
                            Exit Select
                        Case XmlNodeType.Element
                            ' if employee element find no and print
                            If xr.Name = "employee" Then
                                Dim no As String = xr.GetAttribute("no")
                                Console.WriteLine("no=" & no)
                            End If
                            ' if sub elements name or role start accumulating
                            If xr.Name = "name" OrElse xr.Name = "role" Then
                                sb = New StringBuilder()
                            End If
                            Exit Select
                        Case XmlNodeType.EndElement
                            ' if sub elements name or role stop accumulating and print
                            If xr.Name = "name" Then
                                Dim name As String = sb.ToString()
                                Console.WriteLine("name=" & name)
                            End If
                            If xr.Name = "role" Then
                                Dim role As String = sb.ToString()
                                Console.WriteLine("role=" & role)
                            End If
                            Exit Select
                    End Select
                End While
                xr.Close()
            End Using
        End Sub
    End Class
End Namespace
