Using XmlReader.ReadElementContentAs (VB) : XmlReader « XML « ASP.NET Tutorial

ASP.NET Tutorial
1. ASP.Net Instroduction
2. Language Basics
3. ASP.net Controls
4. HTML Controls
5. Page Lifecycle
6. Response
7. Collections
8. Validation
9. Development
10. File Directory
11. Sessions
12. Cookie
13. Cache
14. Custom Controls
15. Profile
16. Configuration
17. LINQ
18. ADO.net Database
19. Data Binding
20. Ajax
21. Authentication Authorization
22. I18N
23. Mobile
24. WebPart
25. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.NET Tutorial » XML » XmlReader 
25. 13. 4. Using XmlReader.ReadElementContentAs (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.Schema

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 bookcount As Integer = 0
        Dim booktotal As Decimal = 0
        Dim settings As New XmlReaderSettings()

        Dim nt As New NameTable()
        Dim book As Object = nt.Add("book")
        Dim price As Object = nt.Add("price")
        settings.NameTable = nt

        Dim booksSchemaFile As String = Path.Combine(Request.PhysicalApplicationPath, "Data.xsd")

        settings.Schemas.Add(Nothing, XmlReader.Create(booksSchemaFile))
        settings.ValidationType = ValidationType.Schema
        settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings

        AddHandler settings.ValidationEventHandler, _
        AddressOf settings_ValidationEventHandler

        settings.IgnoreWhitespace = True
        settings.IgnoreComments = True

        Dim booksFile As String = _
            Path.Combine(Request.PhysicalApplicationPath, "Data.xml")
        Using reader As XmlReader = XmlReader.Create(booksFile, settings)
            While (reader.Read())
                If (reader.NodeType = XmlNodeType.Element _
                    And book.Equals(reader.LocalName)) Then
                    'A subtle, but significant change!
                    bookcount += 1
                End If
                If (reader.NodeType = XmlNodeType.Element And price.Equals(reader.LocalName)) Then
                    booktotal += CType(reader.ReadElementContentAsObject(), Decimal)
                End If

            End While
        End Using
        Response.Write(String.Format("Found {0} books that total {1:C}!", _
                bookcount, booktotal))
    End Sub

    Sub settings_ValidationEventHandler(ByVal sender As Object, _
            ByVal e As System.Xml.Schema.ValidationEventArgs)
        Response.Write(e.Message)
    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>

File: Data.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.books.com" xmlns="http://example.books.com" targetNamespace="http://example.books.com" elementFormDefault="qualified">
  <xsd:element name="bookstore" type="bookstoreType" />
  <xsd:complexType name="bookstoreType">
    <xsd:sequence maxOccurs="unbounded">
      <xsd:element name="book" type="bookType" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="bookType">
    <xsd:sequence>
      <xsd:element name="title" type="xsd:string" />
      <xsd:element name="author" type="authorName" />
      <xsd:element name="price" type="xsd:decimal" />
    </xsd:sequence>
    <xsd:attribute name="genre" type="xsd:string" />
    <xsd:attribute name="publicationdate" type="xsd:string" />
    <xsd:attribute name="ISBN" type="xsd:string" />
  </xsd:complexType>
  <xsd:complexType name="authorName">
    <xsd:sequence>
      <xsd:element name="first-name" type="xsd:string" />
      <xsd:element name="last-name" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
25. 13. XmlReader
25. 13. 1. Processing XML with an XmlReader (C#)
25. 13. 2. Processing XML with an XmlReader (VB)
25. 13. 3. Using XmlReader.ReadElementContentAs (C#)
25. 13. 4. Using XmlReader.ReadElementContentAs (VB)
25. 13. 5. Optimizing XmlReader with a NameTable (C#)
25. 13. 6. Optimizing XmlReader with a NameTable (VB)
25. 13. 7. Validating XML with an XmlReader against an XML Schema (C#)
25. 13. 8. Validating XML with an XmlReader against an XML Schema (VB)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.