Introducing the XMLTextReader


The .NET Framework provides a variety of classes and methods to manipulate XML. However, many times an application requires read only access to an XML Data file. This is the perfect situation for the XMLReader. This abstract class provides a fast forward only cursor for XML files and is used in both the XMLNodeReader and XMlTextReader .NET classes. The XMLTextReader reads records sequentially and allows the skipping of element that may not be needed. Overall, this improves the performance as your traverse the DOM tree.

 

The XMLTextReader provides a cursor that specifies the node in the document. The Read method is used to advance the cursor one node at a time. When initialized the XMLTextReader cursor is positioned at the start of the document. The first call to the Read moves the cursor to the first node in the document. When there are no additional nodes to process it returns a value of false. By default the Read method doesn’t stop on attribute nodes, as they are not considered part of the node structure.

 

For example, if we had an XML file that contained the following data.

 

<customerList>

            <name>Major Company</name>

</customerList>

 

 

The following code shows an example of using the XMLTextReader in a button on a form.

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

 

        Dim xmlFileStream As New FileStream("cust.xml", FileMode.Open)

        Dim xmlRead As New XmlTextReader(xmlFileStream)

 

        While xmlRead.Read

            xmlRead.MoveToContent()

            If xmlRead.HasValue Then

                MsgBox(xmlRead.Value)

            End If

        End While

        xmlRead.Close()

        xmlFileStream.Close()

    End Sub

 

 

Skip to main content