Use the XPath return type to determine how to process the XPath expression : XPath « XML « VB.Net






Use the XPath return type to determine how to process the XPath expression

  
Imports System
Imports System.Xml
Imports System.Xml.XPath

Public Class MainClass

    Public Shared Sub Main()
        Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
        Dim navigator As XPathNavigator = document.CreateNavigator()

        Dim expression1 As XPathExpression = XPathExpression.Compile(".//bk:price/text()*10")  ' Returns a number.
        Dim expression2 As XPathExpression = XPathExpression.Compile("bk:bookstore/bk:book/bk:price")  ' Returns a nodeset.

        Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
        manager.AddNamespace("bk", "http://www.yourname.com/books")

        expression1.SetContext(manager)
        expression2.SetContext(manager)

        Evaluate(expression1, navigator)
        Evaluate(expression2, navigator)

    End Sub

    Public Shared Sub Evaluate(ByVal expression As XPathExpression, ByVal navigator As XPathNavigator)

        Select Case expression.ReturnType
            Case XPathResultType.Number
                Console.WriteLine(navigator.Evaluate(expression))
                Exit Sub

        End Select

    End Sub
End Class

'<bookstore xmlns="http://www.yourname.com/books">
'    <book genre="autobiography" publicationdate="1999-12-12" ISBN="1-111111-11-1">
'        <title>A</title>
'        <author>
'            <first-name>X</first-name>
'            <last-name>Y</last-name>
'        </author>
'        <price>8.99</price>
'    </book>
'    <book genre="novel" publicationdate="2000-01-01" ISBN="0-201-63361-2">
'        <title>Java</title>
'        <author>
'            <first-name>Q</first-name>
'            <last-name>Q</last-name>
'        </author>
'        <price>11.99</price>
'    </book>
'    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
'        <title>C#</title>
'        <author>
'            <name>Author</name>
'        </author>
'        <price>9.99</price>
'    </book>
'</bookstore>

   
    
  








Related examples in the same category

1.Use XPath to do a 'select' commandUse XPath to do a 'select' command
2.Using XPathNavigator to select single node
3.XPathResultType.Number
4.XPathResultType.NodeSet
5.XPathResultType.Boolean
6.XPathResultType.String