Sunday, July 25, 2004

(.NET - VB.NET) How Do I...Read XML from a file in VB.NET

This sample illustrates how to read XML from a file using the XmlTextReader class. This class provides direct parsing and tokenizing of XML, and implements the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML specifications.

XmlReader class is the API that provides XML parsing, the XmlTextReader is the implementation designed to handle byte streams.

Typically, you use the XmlTextReader if you need to access the XML as raw data without the overhead of a DOM. Not having to access the DOM results in a faster way to reading XML. For example, an XML document could have a header section used for routing the document for processing elsewhere. The XmlTextReader has different constructors to specify the location of the XML data. This sample loads XML from the books.xml file, as shown in the following code.

Dim reader As XmlTextReader = New XmlTextReader ("books.xml")

Once loaded, the XmlTextReader moves across the XML data by using the Read method sequentially retrieving the next record from the document. The Read method returns false if there are no more records.

........

No comments: