VB.Net/XML/XPath

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

Use XPath to do a "select" command

 
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Data

Public Class MainClass
    
    Shared Sub Main()
        Dim XDoc As New XmlDocument()
        XDoc.Load("book.xml")
        Dim XNodes As XmlNodeList
        XNodes = XDoc.DocumentElement.SelectNodes( _
                                  "//Book[starts-with(@Name, "C#")]/Order")
        Console.WriteLine("Found {0} Nodes", XNodes.Count)
        Dim XNode As XmlNode
        For Each XNode In XNodes
            Console.WriteLine("Book {0} ordered {1} {2}", _
                     XNode.ParentNode.Attributes.GetNamedItem("Name").Value, _
                     XNode.Attributes.GetNamedItem("Quantity").Value, _
                     XNode.InnerText)
        Next             
    End Sub
End Class


Using XPathNavigator to select single node

  

Public Class Example6
    Shared Sub Main
        Dim doc As System.Xml.XmlDocument
        Dim editor As System.Xml.XPath.XPathNavigator
        doc = New System.Xml.XmlDocument()
        doc.Load("pubs.xml")
        editor = doc.CreateNavigator.SelectSingleNode("/pubs/publishers[@pub_id="0736"]/@pub_name")
        editor.SetValue("new value")
        doc.Save("output.xml")
    End Sub
End Class