VB.Net/XML LINQ/XPath — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 19:40, 26 мая 2010
Loop through the query results and display the information to the screen.
<source lang="vbnet"> Imports System Imports System.Xml.Linq Imports System.Xml.XPath
Public Class MainClass Public Shared Sub Main() Dim employees As XElement = XElement.Load("EmployeesAndTasks.xml") Dim xpathQuery = employees.XPathSelectElements("/Employee/Tasks/Task") For Each task In xpathQuery Console.WriteLine("{0,-15} - {1} ({2})", task.Parent.Parent.<Name>.Value, task.<Name>.Value, task.<Description>.Value) Next End Sub End Class </source>
Selecting node by XPath
<source lang="vbnet"> Public Class MainClass
Shared Sub Main() Dim file As String = "books.xml" Dim doc As New System.Xml.XmlDocument doc.Load(file) Dim nodes As System.Xml.XmlNodeList nodes = doc.SelectNodes("shelf/book") Dim counter = 0 Do Until counter = nodes.Count System.Console.WriteLine(nodes.Item(counter).SelectSingleNode("title").InnerText & " by " & nodes.Item(counter).SelectSingleNode("author").InnerText & vbCrLf) counter = counter + 1 Loop End Sub
End Class
</source>
Use XPath to get the tasks for each employee.
<source lang="vbnet"> Imports System Imports System.Xml.Linq Imports System.Xml.XPath
Public Class MainClass Public Shared Sub Main() Dim employees As XElement = XElement.Load("EmployeesAndTasks.xml") " Dim xpathQuery = employees.XPathSelectElements("/Employee/Tasks/Task") End Sub End Class </source>