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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » XML » XPathScreenshots 
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.