Udostępnij za pośrednictwem


Reading XML Data with XmlTextReader

The XmlTextReader class is an implementation of XmlReader and provides a parser over XML text. It enforces the rules that XML must be well-formed. It is neither a validating nor a non-validating parser, since it does not use document type definition (DTD) or schema information. It can read text in blocks or read characters from a stream.

Note

In the .NET Framework version 2.0, the recommended practice is to create XmlReader instances using the XmlReaderSettings class and the Create method. This allows you to take full advantage of all the new features introduced in the .NET Framework 2.0. For more information, see Creating XML Readers.

Overview

The XmlTextReader provides the following functionality:

  • Enforces the rules that XML must be well-formed.

  • Checks that the DTD is well-formed. However, it does not use the DTD for validation or adding default attributes. The DTD is used for entity expansion if the EntityHandling property is set to ExpandEntities, or if the ResolveEntity method is called on an EntityReference node.

  • Returns unexpanded entity references as EntityReference nodes when the EntityHandling property is set to ExpandCharEntities (this is the default setting). If the entity reference is not defined in a DTD, the reader does not error unless the ResolveEntity method is called on the node.

  • Provides an option to turn off XML normalization of new lines and attribute values. Normalization is turned off by default.

  • Validating is not done against DTDs or schemas.

  • Provides a fast XML parser, because the XmlTextReader does not have the overhead involved with validation checking.

The XmlTextReader can read data from different inputs, such as a stream object, a TextReader, and a URL identifying a local file location or Web site.

The XmlTextReader uses an XmlResolver to locate external resources, such as DTDs, so it can check DTDs to see if they are well-formed. For more information on the XmlResolver, see Resolving Resources Using the XmlResolver.

The encoding declaration, <?xml version="1.0" encoding="ISO-8859-5"?>, contains an encoding attribute that sets the encoding for your document. The XmlTextReader has an Encoding property that returns the character encoding found in the encoding attribute in the XML declaration. If no encoding attribute is found, the default for the document is set to UTF-8 or UTF-16 based on the first two bytes of the stream.

If an external resource is read, such as a DTD used to expand an entity reference or a schema file, the encoding is set to the encoding value found in the external reference. If no encoding is found in the external reference, the default is set to UTF-8 or UTF-16. The XmlTextReader supports many encodings, as it uses the Encoding class. Therefore, all encodings supported by that class are also supported by the XmlTextReader. The only encodings not supported are ones that map the <?xml sequence to different byte values than UTF-8, like UTF-7 and EBCDIC.

See Also

Concepts

Reading XML with the XmlReader

Other Resources

Using the XmlReader Class