Mega Code Archive

 
Categories / ASP.Net Tutorial / XML
 

Querying XML with XPathDocument and XPathNodeIterator (VB)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"  Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Untitled Page</title> </head> <body>     <form id="form1" runat="server">     <div>          </div>     </form> </body> </html> File: Default.aspx.vb Imports System.IO Imports System.Xml Imports System.Xml.XPath Partial Class _Default     Inherits System.Web.UI.Page     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _            Handles Me.Load         Dim booksFile As String = Path.Combine(Request.PhysicalApplicationPath, _             "Data.xml")         Dim document As New XPathDocument(booksFile)         Dim nav As XPathNavigator = document.CreateNavigator()         Dim namespaceMgr As New XmlNamespaceManager(nav.NameTable)         namespaceMgr.AddNamespace("b", "http://example.books.com")         For Each node As XPathNavigator In nav.Select( _                 "//b:book[not(b:price[. > 10.00])]/b:price", namespaceMgr)             Dim price As Decimal = _                 CType(node.ValueAs(GetType(Decimal)), Decimal)             Response.Write(String.Format("Price is {0}<BR/>", _                 price))         Next     End Sub End Class File: Data.xml <?xml version='1.0'?> <bookstore xmlns="http://example.books.com"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">     <book genre="A"            publicationdate="1981"            ISBN="1-11111-11-0">         <title>title 1</title>         <author>             <first-name>A</first-name>             <last-name>B</last-name>         </author>         <price>8</price>     </book>     <book genre="B"            publicationdate="1999"            ISBN="0-222-22222-2">         <title>title 2</title>         <author>             <first-name>C</first-name>             <last-name>D</last-name>         </author>         <price>11.99</price>     </book> </bookstore>