Use the SetContext method to provide an XmlNamespaceManager object for namespace resolution. : Namespace « XML « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » XML » Namespace 
25.10.1.Use the SetContext method to provide an XmlNamespaceManager object for namespace resolution.
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>
25.10.Namespace
25.10.1.Use the SetContext method to provide an XmlNamespaceManager object for namespace resolution.
25.10.2.Read an XML fragment with XmlParserContext and its XmlNamespaceManager
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.