You can use the XmlTextReader class when you want to read a XML document in your code. The XmlTextReader moves through your document from top to bottom, one node at a time. You call the Read() method to move to the next node. In a typical application, you would need to go fishing for the elements that interest you. For example, you might read information from an XML file such as BooksList.xml and use it to create Book objects based on the Book class shown here:
Public Class Book
Private m_Isbn13 As String
Private m_Title As String
Private m_Author As String
Private m_Price As Decimal
Private m_Available As Boolean
Public Property Isbn13() As String
Get
Return m_Isbn13
End Get
Set(value As String)
m_Isbn13 = value
End Set
End Property
Public Property Title() As String
Get
Return m_Title
End Get
Set(value As String)
m_Title = value
End Set
End Property
Public Property Author() As String
Get
Return m_Author
End Get
Set(value As String)
m_Author = value
End Set
End Property
Public Property Price() As Decimal
Get
Return m_Price
End Get
Set(value As Decimal)
m_Price = value
End Set
End Property
Public Property Available() As Boolean
Get
Return m_Available
End Get
Set(value As Boolean)
m_Available = value
End Set
End Property
End Class
Note: You can find how BooksList.xml is created from the article How to use XMLTextWriter to create an XML document in VB.NET.
A typical application might read data from an XML file and place it directly into the corresponding objects. The next example shows how you can easily create a group of Book objects based on the BookstList.xml file. This example uses the generic List collection, so you should import the System.Collections.Generic namespace.
The .aspx file used in this example is
<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”XMLReaderV2.aspx.vb” Inherits=”WriteToXMLFileVB.XMLReaderV2″ %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”https://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:GridView ID=”GridResult” runat=”server”>
</asp:GridView>
</div>
</form>
</body>
</html>
The code behind in VB.NET is:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Xml
Public Class Book
Private m_Isbn13 As String
Private m_Title As String
Private m_Author As String
Private m_Price As Decimal
Private m_Available As Boolean
Public Property Isbn13() As String
Get
Return m_Isbn13
End Get
Set(value As String)
m_Isbn13 = value
End Set
End Property
Public Property Title() As String
Get
Return m_Title
End Get
Set(value As String)
m_Title = value
End Set
End Property
Public Property Author() As String
Get
Return m_Author
End Get
Set(value As String)
m_Author = value
End Set
End Property
Public Property Price() As Decimal
Get
Return m_Price
End Get
Set(value As Decimal)
m_Price = value
End Set
End Property
Public Property Available() As Boolean
Get
Return m_Available
End Get
Set(value As Boolean)
m_Available = value
End Set
End Property
End Class
Partial Public Class XMLReaderV2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
XMLFileReader()
End If
End Sub
Protected Sub XMLFileReader()
‘ Open a stream to the file.
Dim File As String = Path.Combine(Request.PhysicalApplicationPath, “App_Data\BooksList.xml”)
Dim Fs As New FileStream(File, FileMode.Open)
Dim R As New XmlTextReader(Fs)
‘ Create a generic collection of books.
Dim Books As New List(Of Book)()
‘ Loop through the books.
While R.Read()
If R.NodeType = XmlNodeType.Element AndAlso R.Name = “Book” Then
Dim newBook As New Book()
newBook.Isbn13 = R.GetAttribute(“ISBN-13”)
newBook.Title = R.GetAttribute(“Title”)
‘ Get the rest of the subtags for this book
While R.NodeType <> XmlNodeType.EndElement
R.Read()
‘ Look for Author subtags.
If R.Name = “Author” Then
While R.NodeType <> XmlNodeType.EndElement
R.Read()
If R.NodeType = XmlNodeType.Text Then
newBook.Author = R.Value
End If
End While
End If
R.Read()
If R.Name = “Price” Then
While R.NodeType <> XmlNodeType.EndElement
R.Read()
If R.NodeType = XmlNodeType.Text Then
newBook.Price = [Decimal].Parse(R.Value)
End If
End While
End If
R.Read()
If R.Name = “Available” Then
While R.NodeType <> XmlNodeType.EndElement
R.Read()
If R.NodeType = XmlNodeType.Text Then
newBook.Available = [Boolean].Parse(R.Value)
End If
End While
End If
End While
Books.Add(newBook)
End If
End While ‘ end of while
Fs.Close()
GridResult.DataSource = Books
GridResult.DataBind()
End Sub
End Class
Notes:
– This code uses a nested looping structure. The outside loop iterates over all the books, and the inner loop searches through all the child elements of <Book>.
– The EndElement node alerts you when a node is complete and the loop can end. Once all the information is read for a product, the corresponding object is added into the collection.
– All the information is retrieved from the XML file as a string. Thus, you need to use methods like Decimal.Parse or Boolean.Parse to convert it to the right data type.
– Data binding is used to display the contents of the collection. A GridView set to generate columns automatically creates the table shown in the next picture.