VB.Net/XML LINQ/XPath — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Loop through the query results and display the information to the screen.

  
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


Selecting node by XPath

  
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


Use XPath to get the tasks for each employee.

  
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