日期:2008-11-20  浏览次数:20546 次

HOW TO: Read XML Data from a Stream
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about obtaining support for a Beta release, please see the documentation included with the Beta product files, or check the Web location from which you downloaded the release.
--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft .NET Development Platform (NDP) Beta 2

--------------------------------------------------------------------------------

IN THIS TASK
SUMMARY
Requirements
How to Read XML Data from a Stream
REFERENCES


SUMMARY
This article demonstrates how to use the XmlTextReader class to read Extensible Markup Language (XML) from a stream. The stream can come from a variety of sources, such as a byte stream from a server, a file, or a TextReader class.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
XML terminology
Creating and reading XML
How to Read XML Data from a Stream
Open Visual Studio .NET.


Create a new Microsoft Visual Basic (VB) or Microsoft Visual C# Console Application.

NOTE : The following steps provide a detailed description of how to build the application. You can also go directly to step 10, where completed code is provided.


Make sure that the project contains a reference to the System.Xml and System.IO namespace.


Use the IMPORTS statement on the Xml namespace so that you are not required to qualify XmlTextReader declarations in that namespace later in your code. You must use the IMPORTS statement prior to any other declarations as follows:


Visual Basic .NET code
Imports System.Xml
Imports System.IO
Visual C# code
using System.Xml;
using System.IO;
Create or retrieve the XML stream. A stream is an abstract representation of an input or output device that is the source of, or destination for, data (in this case, XML data). You can write to and read from a stream, which is best visualized as a flow of bytes.

Streams are used to provide independence from the device and therefore require no program changes if, for example, the source of a stream changes. There are a few different ways to create a stream for the XmlTextReader class. Select one of the following code samples to add to the Main procedure of the default module:


Code sample that uses the StringReader object:

The StringReader object reads characters from strings and takes in a string value during construction.

Visual Basic .NET Code
Dim stream as System.IO.StringReader
stream = new StringReader("<?xml version='1.0'?>" & _
"<!-- This file is a book store inventory. -->" & _
"<bookstore>" & _
" <book genre=""autobiography"" ISBN=""1-861003-11-0"">" & _
"   <title>The Autobiography of Benjamin Franklin</title>" & _
"   <author>" & _
"       <first-name>Benjamin</first-name>" & _
"       <last-name>Franklin</last-name>" & _
"   </author>"