theSince in the last post an example of System.Xml.XmlTextWriter was used to write an XML-document this article will show how to read an XML-document.
Private Shared Sub LoadXMLfile(ByVal Path string)
If My.Computer.FileSystem.FileExists(Path) Then
Dim rdrXML As New System.Xml.XmlTextReader(Path) //Consturctor with XML file's path
Try
rdrXML.MoveToContent()
Dim ElementName As String = ""
Dim NextItem As Boolean = True
Dim objListViewItem As Favoritenobjekt = Nothing
Do While rdrXML.Read
If NextItem Then
objListViewItem = New Favoritenobjekt
NextItem = False
End If
Select Case rdrXML.NodeType
Case System.Xml.XmlNodeType.Element
ElementName = rdrXML.Name
Case System.Xml.XmlNodeType.Text
If ElementName = "Name" Then
objListViewItem.Text = rdrXML.Value
End If
If ElementName = "Kategorie" Then
objListViewItem.Kategorie = rdrXML.Value
End If
If ElementName = "URL" Then
objListViewItem.URL = rdrXML.Value
Favoriten_Liste.Add(objListViewItem)
NextItem = True
End If
End Select
Loop
rdrXML.Close()
rdrXML = Nothing
Catch ex As System.Exception
MsgBox("Error. Xmlfile could not be loaded", MsgBoxStyle.Critical)
End Try
End If
End Sub
What is important in the code?
The XMLTextReader is constucted with the path to the file which you are trying to read. rsrXMl.MoveToContent Points the Reader to the Data. Since you do not know how much information is in the file you have to use a while loop. Important in this way of reading the data is that we are first reading an node then, we check if it is an Element or Text. The Elementname is stored in 'Elementname' which is needed to identify the element the Text (case 2) belongs to.
Keine Kommentare:
Kommentar veröffentlichen