The release number of this article has been CHS301228
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.xml system.io
This task content
summary
Require how to read XML data reference in data streams
Summary This article demonstrates how to use
The XMLTextReader class reads the extended tag language (XML) from the stream. The stream can come from various sources, such as from servers, files or
The byte stream of the TextReader class.
Back to top
Requirements The recommended hardware, software, network structure, and service pack required:
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 Create and Read XML
How to read XML data in the data stream
Open Visual Studio .NET. New Microsoft Visual Basic (VB) or Microsoft Visual C # console application. Note: The following steps provide a detailed description of how to generate an application. You can also directly go directly to step 10, and a complete code is provided in this step. Make sure the project contains references to System.xml and System.io namespace. Use the Imports statement for the XML namespace, so that there is no need to define the XMLTextReader declaration in the namespace in the later code. The Imports statement must be as follows before all other declarations. Visual Basic .NET Code Imports System.xml
Imports System.iovisual C # code Using System.xml;
Using system.io; create or retrieve XML data streams. The stream is an abstract concept that represents an input or output device for use as data (here XML data) source or target. You can write a stream or read from the stream, and the flow can be most imagerable to the flow of bytes. Data flow is used independently of the device, so if the source of the data stream changes, the program is not required to change. There are several different ways to create streams to the XMLTextReader class. Select One of the following code examples to the main process:
Using the code example of the StringReader object: StringReader object reads characters from the string, get a string value during the construction process. Visual Basic .NET code DIM stream as system.io.stringReader
stream = new stringReader (" XML Version = '1.0'?>" & _
"
"
"
author> "& _
book> "& _
" bookstore>") C # code StringReader Stream;
stream = new stringReader (" xml version = '1.0'?>"
"
"
"
"Author>"
"
author> "
" book>"
"
"Author>"
"
author> "
" book>"
"
author> "
"Price> 9.99 price>"
" book>"
" BookStore>"); Use the code example of the StreamReader object: StreamReader object is used to read characters from the file. During the construction, the object reads the name of the file to be read: Visual Basic .NET code DIM stream as system.io.streamReader
'Loadings the xml data in the file books.xml in a new stream.
Stream = New StreamReader ("Books.xml") C # code system.io.streamReader stream = new system.io.StreamReader ("Books.xml"); please note that you use the books.xml file here. You can create your own books.xml file. Example books.xml files are provided with Visual Studio .NET and .NET Framework SDK. Use a data stream to construct an XMLTextReader class. Typically, if you need to use XML as the original data to be accessed without using the Document Object Model (DOM), you can use XMLTextReader; therefore, XMLTextReader provides a mechanism for reading XML faster. XMLTextReader uses a different constructor to specify the location of XML data. The following code loaded from a stream XMLTextReader: Visual Basic .NET code DIM Reader as XmlTextReader = New XMLTextReader (Stream) C # code XMLTextReader Reader = NULL;
Reader = New XMLTextReader (Stream); Read all XML data. After loading, XMLTextReader will read XML data continuously and use the Read method to obtain the next record. After arriving at the last record, return false. Visual Basic .NET code do while (ready)
'Do some work here on the data.
Console.writeline (Reader.Name)
Loop
'Reading of the xml file has finished.
Console.readline () 'Pausec # code while (reader.read ())
{
// Do some work here on the data.
...
}
While (Reader.Read ())
{
// Do some work here on the data.
Console.writeLine (Reader.Name);
}
Console.readline (); check node. To process XML data, each record should have a node type, which can determine the node type through the NodeType property. Name and Value Attributes Returns the node name (element and attribute name) of the current node (or record) and node value (node text). NodeType enumerates determines the node type. The following example shows the name and document type of the element. Note that this example ignores the element properties. Visual Basic .NET code do while (reader.Read ()) Select Case Reader.NodeType
Case XMLNodetype.ement 'Display Beginning of Element.
Console.write ("<" Reader.name)
Console.writeline (">")
Case XMLNodType.text 'Display The Text in Each Element.
Console.writeline (Reader.Value)
Case XMLNodetype.Endelement 'Display end of element.
Console.write ("" ready.name)
Console.writeline (">")
End SELECT
Loopc # code while (reader.Read ())
{
Switch (Reader.NodeType)
{
Case XMLNodetype.Element: // The node is an element.
Console.write ("<" Reader.Name);
Console.writeline (">");
Break;
Case XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodetype.Endelement: // Display end of element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
} Check the properties. Element node types can include a series of attribute nodes associated therewith. The MoveTonextAttribute method is moved in each attribute of the element. Use the Hasattribute attribute to detect whether the node has any properties. AttributeCount properties Returns the number of properties of the current node. Visual Basic .NET code do while (ready)
Select Case Reader.NodeType
Case XMLNodetype.ement 'Display Beginning of Element.
Console.write ("<" Reader.name)
If Reader.haSattributes Then 'if attributes exist
While reader.movetonextAtttribute ()
'Display Attribute Name and value.
Console.write ("{0} = '{1}'", reader.name, reader.value)
End while
END IF
Console.writeline (">")
Case XMLNodType.text 'Display The Text in Each Element.
Console.writeline (Reader.Value)
Case XMLNodetype.Endelement 'Display end of element.
Console.write ("" ready.name)
Console.writeline (">")
End SELECT
Loopc # code while (reader.Read ())
{
Switch (Reader.NodeType)
{
Case XMLNodetype.Element: // The node is an element.
Console.write ("<" Reader.Name);
While (Reader.MovetonextAttRibute ()) // read attributes.
Console.write ("" Reader.Name "= '" Reader.Value "');
Console.write (">");
Console.writeline (">");
Break;
Case XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodetype.Endelement: // Display end of element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
} For convenience, this will provide a complete code. Visual Basic.net code IMPORTS System.xml
Imports system.io
Module Module1
Sub main ()
Dim Stream as system.io.streamReader
'Loadings the xml data in the file books.xml in a new stream.
Stream = New StreamReader ("Books.xml")
Dim Reader As XmlTextReader = New XMLTextReader (Stream)
Do while (reader.read ())
Select Case Reader.NodeType
Case XMLNodetype.ement 'Display Beginning of Element.
Console.write ("<" Reader.name)
If Reader.haSattributes Then 'if attributes exist
While reader.movetonextAtttribute ()
'Display Attribute Name and value.
Console.write ("{0} = '{1}'", reader.name, reader.value)
End while
END IF
Console.writeline (">")
Case XMLNodType.text 'Display The Text in Each Element.
Console.writeline (Reader.Value) Case XMLNodetype.Endelement 'Display End of Element.
Console.write ("" ready.name)
Console.writeline (">")
End SELECT
Loop
End Sub
End modulec # code using system;
USING SYSTEM.XML;
Using system.io;
Namespace ReadXmlFromstream
{
///
/// summary description for class1.
/// summary>
Class class1
{
Static void main (string [] args)
{
System.io.StreamReader stream = new system.io.streamReader ("Books.xml");
XmlTextReader Reader = NULL;
Reader = New XMLTextReader (stream);
While (Reader.Read ())
{
Switch (Reader.NodeType)
{
Case XMLNodetype.Element: // The node is an element.
Console.write ("<" Reader.Name);
While (Reader.MovetonextAttRibute ()) // read attributes.
Console.write ("" Reader.Name "= '" Reader.Value "');
Console.write (">");
Console.writeline (">");
Break;
Case XMLNodType.Text: // Display The Text in Each Element.
Console.writeline (Reader.Value);
Break;
Case XMLNodetype.Endelement: // Display end of element.
Console.write ("" ready.name;
Console.writeline (">");
Break;
}
}
}
}
}
Generate and run your project.
Back to top
Refer to more information, see "XML IN Microsoft .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation" (XML: .NET Framework XML class and C # provide simple, scalable) Data operation) One article, this article is
In MSDN Magazine, you can access from the following Microsoft Web site:
Http://msdn.microsoft.com/library/periodic/period01/xml0101.htm
XmlReader,
StreamReader and
For more information on the StringReader class, see the Microsoft .NET Framework Class library document.
Use
For more information on XMLReader read XML data, see the Microsoft .NET Framework Developer's Guide document. Back to top
The information in this article applies to:
Microsoft .NET framework
Recent Update: 2002-6-25 (1.0) Keyword Kbhowto KbhowTomaster KBXML TSLIC_TSLIC KB301228 KBAUDDEVELOPER