12.2 Use XMLReader to Read an XML Document
In How-To 12.1, I learned how to write out data to an XML document by using the
XMLWriter. How do I read data using the XMLReader?
Technique
Whereas the XMLWriter has one implementation (class), the XMLReader has three,
depending on the task you need to perform. Those classes are listed in Table 12.4.
Table 12.4. XMLWriter Implementations
Class Name Purpose/Descriptions
XMLTextReader Reads character streams. This is a forward-only reader that has
methods returning data on content and node types. No
validation occurs.
XMLNodeReader Provides a parser over an XML Document Object Model
(DOM) API, similar to the XMLNode tree.
XMLValidatingReader Takes an XMLTextReader adding validation. In doing so, it
provides a fully compliant validating or non-validating XML
parser with DTD, XSD schema, or XML-Data Reduced (XDR)
schema support.
For this How-To, you will be using the XMLTextReader to read the file and display the
results. To do this, you will use the following methods:
•
Read. Reads a line of the character stream out of the XMLReadReader.
•
AttributeCount. Gives the count of attributes. This depends on the type of data that
is being read. In this case, AttributeCount is used to display the individual
attributes, which will be columns.
•
NodeType. Allows you to specify node types in the XML document. In the case of
this How-To, the System.Xml.XmlNodeType.XmlDeclaration is checked for so
that no information is printed for that type of node. You can see the other node
types by looking at intellisense for the NodeType property.
•
Value. Represents the actual value of the line in the stream.
•
Close. Closes the stream.
Steps
Open and run the Visual Basic .NET-Chapter 12 solution. From the main Web page,
click on the hyperlink with the caption How-To 12.2: Use XMLReader to Read an XML
Document. When the page loads, click the button labeled Read XML File. The example
then reads the XML file that is specified in the text box labeled File to Read and displays
the information in the text area located at the bottom of the form (see Figure 12.2).
1. Create a Web Form. Then place the Label, TextBox, and Button objects as seen in
Figure 12.2 on the form with the following properties set as in Table 12.5.
Table 12.5. Label, TextBox, and Button Control Property Settings
Object Property Setting
Label Text File to Read
TextBox ID txtFileToRead
Button ID btnReadFile
Text Read XML File
TextArea ID taOutput
HyperLink ID hplReturnToMain
NavigateURL wfrmMain.aspx
2. Note
You will find the TextArea control in the HTML Components
section of the toolbox. After you have dragged this control onto
the Web Form, right-click and choose Run as Server Control from
the pop-up menu. This will then run this control as a server side
control, and you will be able to work with its properties and
methods in code behind.
3. Add the code in Listing 12.5 to the Click event of btnReadFile. To start off, the
XXLTextReader is initialized with the XML document specified in txtFileToRead.
Then each node of the document is read using the Read method. The node type is
compared to make sure the current node is not the XMLDeclaration. If it's not,
then each of the attributes is displayed; in this case, the columns of each name are
entered. After each of the nodes (rows) have been read and added to strOut, then
that string is assigned to the InnerText property of the TextArea object called
taOutput. Last, the XMLTextReader object is closed.
Listing 12.5 wfrmHowTo12_2.aspx.vb: Reading an XML Document Using
XMLTextReader
Private Sub btnReadFile_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim xtrNames As System.Xml.XmlTextReader
Dim strOut As String
Dim intAtts As Integer
Dim intCurrAtt As Integer
Try
strOut = "Reading file " & _
Me.txtFileToRead.Text & "..." & vbCrLf
xtrNames = New System.Xml.XmlTextReader(Me.txtFileToRead.Text)
While xtrNames.Read()
intAtts = xtrNames.AttributeCount
If xtrNames.NodeType <> System.Xml.XmlNodeType.XmlDeclaration
Then
If intAtts > 0 Then
For intCurrAtt = 0 To intAtts - 1
strOut &= xtrNames(intCurrAtt) & " "
Next
Else
strOut &= xtrNames.Value
End If
End If
End While
Catch excp As Exception
strOut &= "Following Error Occurred: " & excp.Message
Finally
strOut &= vbCrLf & "Done Processing " & Me.txtFileToRead.Text & ""
taOutput.InnerText = strOut
If Not xtrNames Is Nothing Then
xtrNames.Close()
End If
End Try
End Sub
Figure 12.2. The information displayed here was read from an XML document
using XMLReader.
Comments
Again, as with XMLTextReader, this falls into the middle of complexity when it comes
to reading XML documents. If you want to actually validate the data, then you should use
the XMLValidatingReader implementation of the XMLReader, rather than the
XMLTextReader.