|
Tipp 0162
|
XML-Datei gezielt auslesen
|
|
|
Autor/Einsender: Datum: |
|
Michael Werner 18.05.2007 |
|
Entwicklungsumgebung: |
|
VB.Net 2005 |
Framework: |
|
2.0 |
|
|
Alle Items eines XML-Documentes auszulesen, ist im allgemeinen bekannt
und recht einfach umzusetzen. Eine XML-Datei wird in eine XMLDocument-Instanz geladen (Load) und dann ausgelesen.
|
Das gezielte Abfragen einzelner Elemente und deren jeweiligen Unterelemente aus einer wohlgeformten XML-Datei wird im Tipp über Instanzen von
XPathDocument, XPathNavigator und XPathExpression, XPathNodeIterator realisiert.
|
|
|
Imports System.Xml
Imports System.Xml.XPath
Public Class Form1
Dim sXmlFile As String = "..\..\resources\kanzler.xml"
Private Sub XPathNavigator_SelectElement(ByVal element As String)
'Element mit Unterelementen gezielt auslesen
Dim document As XPathDocument = New XPathDocument(sXmlFile)
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim query As XPathExpression = _
navigator.Compile("/bundeskanzler/kanzler")
Dim nodes As XPathNodeIterator = navigator.Select(query)
Dim nodesNavigator As XPathNavigator = nodes.Current
Dim nodesText As XPathNodeIterator = _
nodesNavigator.SelectDescendants(XPathNodeType.Text, False)
Dim i As Integer
If CheckBox1.Checked = False Then
ListBox1.Items.Clear()
End If
While nodesText.MoveNext()
If i > 0 AndAlso i < 3 Then
ListBox1.Items.Add(nodesText.Current.Value)
i += 1
End If
If nodesText.Current.Value = element Then
ListBox1.Items.Add(nodesText.Current.Value)
i += 1
End If
End While
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
ComboBox1.SelectedIndexChanged
XPathNavigator_SelectElement(ComboBox1.SelectedItem.ToString)
End Sub
Private Sub XPathNavigator_SelectAll()
'Alle Elemente auslesen
Dim document As XPathDocument = New XPathDocument(sXmlFile)
Dim navigator As XPathNavigator = document.CreateNavigator()
Dim query As XPathExpression = _
navigator.Compile("/bundeskanzler/kanzler")
Dim nodes As XPathNodeIterator = navigator.Select(query)
Dim nodesNavigator As XPathNavigator = nodes.Current
Dim nodesText As XPathNodeIterator = _
nodesNavigator.SelectDescendants(XPathNodeType.Text, False)
ListBox1.Items.Clear()
While nodesText.MoveNext()
ListBox1.Items.Add(nodesText.Current.Value)
End While
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
XPathNavigator_SelectAll()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
With ComboBox1
.Items.Add("Konrad Adenauer")
.Items.Add("Ludwig Erhard")
.Items.Add("Kurt Georg Kiesinger")
.Items.Add("Willy Brandt")
.Items.Add("Helmut Schmidt")
.Items.Add("Helmut Kohl")
.Items.Add("Gerhard Schroeder")
.Items.Add("Angela Merkel")
.SelectedIndex = 0
End With
Label1.Text = "Lebens- und Regierungszeiten der Bundeskanzler"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
ListBox1.Items.Clear()
End Sub
End Class
|
|
|
|
|
Windows-Version |
98/SE |
|
|
ME |
|
|
NT |
|
|
2000 |
|
|
XP |
|
|
Vista |
|
|
Win
7 |
|
|
|
|
Download (19,3
kB)
|
Downloads bisher: [ 772 ]
|
|
|