XmlTextReader Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a reader that provides fast, non-cached, forward-only access to XML data.
We recommend that you use the XmlReader class instead.
public ref class XmlTextReader : System::Xml::XmlReader, System::Xml::IXmlLineInfo, System::Xml::IXmlNamespaceResolver
public ref class XmlTextReader : System::Xml::XmlReader, System::Xml::IXmlLineInfo
public class XmlTextReader : System.Xml.XmlReader, System.Xml.IXmlLineInfo, System.Xml.IXmlNamespaceResolver
public class XmlTextReader : System.Xml.XmlReader, System.Xml.IXmlLineInfo
type XmlTextReader = class
inherit XmlReader
interface IXmlLineInfo
interface IXmlNamespaceResolver
type XmlTextReader = class
inherit XmlReader
interface IXmlLineInfo
Public Class XmlTextReader
Inherits XmlReader
Implements IXmlLineInfo, IXmlNamespaceResolver
Public Class XmlTextReader
Inherits XmlReader
Implements IXmlLineInfo
- Inheritance
- Implements
Remarks
Note
We recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.
XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node.
This class implements XmlReader and conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations. XmlTextReader provides the following functionality:
Enforces the rules of well-formed XML.
XmlTextReaderdoes not provide data validation.Checks that
DocumentTypenodes are well-formed.XmlTextReaderchecks the DTD for well-formedness, but does not validate using the DTD.For nodes where NodeType is
XmlNodeType.EntityReference, a single emptyEntityReferencenode is returned (that is, the Value property isString.Empty).
Note
The actual declarations of entities in the DTD are called Entity nodes. When you refer to these nodes in your data, they are called EntityReference nodes.
- Does not expand default attributes.
Because the XmlTextReader does not perform the extra checks required for data validation, it provides a fast well-formedness parser.
To perform data validation, use a validating XmlReader.
To read XML data from an XmlDocument, use XmlNodeReader.
XmlTextReader throws an XmlException on XML parse errors. After an exception is thrown the state of the reader is not predictable. For example, the reported node type may be different than the actual node type of the current node. Use the ReadState property to check whether a reader is in error state.
Security considerations
The following are things to consider when using the XmlTextReader class.
Exceptions thrown by the XmlTextReader can disclose path information that you do not want bubbled up to the application. Your applications must catch exceptions and process them appropriately.
DTD processing is enabled by default. Disable DTD processing if you are concerned about Denial of Service issues or if you are dealing with untrusted sources. Set the DtdProcessing property to Prohibit to disable DTD processing.
If you have DTD processing enabled, you can use ThrowingResolver to restrict the resources that the XmlTextReader can access. You can also design your application so that the XML processing is memory and time constrained. For example, configure time-out limits in your ASP.NET application.
XML data can include references to external resources such as a DTD file. By default external resources are resolved using an XmlUrlResolver object with no user credentials. You can secure this further by doing one of the following:
- Restrict the resources that the XmlTextReader can access by using ThrowingResolver.
- Do not allow the XmlReader to open any external resources by setting the XmlResolver property to
null.
XML data can contain a large number of attributes, namespace declarations, nested elements and so on that require a substantial amount of time to process. To limit the size of the input that is sent to the XmlTextReader, create a custom IStream implementation and supply it the XmlTextReader.
The ReadValueChunk method can be used to handle large streams of data. This method reads a small number of characters at a time instead of allocating a single string for the whole value.
By default general entities are not expanded. General entities are expanded when you call the ResolveEntity method.
Constructors
| Name | Description |
|---|---|
| XmlTextReader() |
Initializes a new instance of the |
| XmlTextReader(Stream, XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified stream and XmlNameTable. |
| XmlTextReader(Stream, XmlNodeType, XmlParserContext) |
Initializes a new instance of the XmlTextReader class with the specified stream, XmlNodeType, and XmlParserContext. |
| XmlTextReader(Stream) |
Initializes a new instance of the XmlTextReader class with the specified stream. |
| XmlTextReader(String, Stream, XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified URL, stream and XmlNameTable. |
| XmlTextReader(String, Stream) |
Initializes a new instance of the XmlTextReader class with the specified URL and stream. |
| XmlTextReader(String, TextReader, XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified URL, TextReader and XmlNameTable. |
| XmlTextReader(String, TextReader) |
Initializes a new instance of the XmlTextReader class with the specified URL and TextReader. |
| XmlTextReader(String, XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified file and XmlNameTable. |
| XmlTextReader(String, XmlNodeType, XmlParserContext) |
Initializes a new instance of the XmlTextReader class with the specified string, XmlNodeType, and XmlParserContext. |
| XmlTextReader(String) |
Initializes a new instance of the XmlTextReader class with the specified file. |
| XmlTextReader(TextReader, XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified TextReader and XmlNameTable. |
| XmlTextReader(TextReader) |
Initializes a new instance of the XmlTextReader class with the specified TextReader. |
| XmlTextReader(XmlNameTable) |
Initializes a new instance of the XmlTextReader class with the specified XmlNameTable. |
Properties
| Name | Description |
|---|---|
| AttributeCount |
Gets the number of attributes on the current node. |
| BaseURI |
Gets the base URI of the current node. |
| CanReadBinaryContent |
Gets a value indicating whether the XmlTextReader implements the binary content read methods. |
| CanReadValueChunk |
Gets a value indicating whether the XmlTextReader implements the ReadValueChunk(Char[], Int32, Int32) method. |
| CanResolveEntity |
Gets a value indicating whether this reader can parse and resolve entities. |
| CanResolveEntity |
Gets a value indicating whether this reader can parse and resolve entities. (Inherited from XmlReader) |
| Depth |
Gets the depth of the current node in the XML document. |
| DtdProcessing |
Gets or sets the DtdProcessing enumeration. |
| Encoding |
Gets the encoding of the document. |
| EntityHandling |
Gets or sets a value that specifies how the reader handles entities. |
| EOF |
Gets a value indicating whether the reader is positioned at the end of the stream. |
| HasAttributes |
Gets a value indicating whether the current node has any attributes. (Inherited from XmlReader) |
| HasValue |
Gets a value indicating whether the current node can have a Value other than |
| IsDefault |
Gets a value indicating whether the current node is an attribute that was generated from the default value defined in the DTD or schema. |
| IsEmptyElement |
Gets a value indicating whether the current node is an empty element (for example, |
| Item[Int32] |
Gets the value of the attribute with the specified index. |
| Item[Int32] |
When overridden in a derived class, gets the value of the attribute with the specified index. (Inherited from XmlReader) |
| Item[String, String] |
Gets the value of the attribute with the specified local name and namespace URI. |
| Item[String, String] |
When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI. (Inherited from XmlReader) |
| Item[String] |
Gets the value of the attribute with the specified name. |
| Item[String] |
When overridden in a derived class, gets the value of the attribute with the specified Name. (Inherited from XmlReader) |
| LineNumber |
Gets the current line number. |
| LinePosition |
Gets the current line position. |
| LocalName |
Gets the local name of the current node. |
| Name |
Gets the qualified name of the current node. |
| Namespaces |
Gets or sets a value indicating whether to do namespace support. |
| NamespaceURI |
Gets the namespace URI (as defined in the W3C Namespace specification) of the node on which the reader is positioned. |
| NameTable |
Gets the XmlNameTable associated with this implementation. |
| NodeType |
Gets the type of the current node. |
| Normalization |
Gets or sets a value indicating whether to normalize white space and attribute values. |
| Prefix |
Gets the namespace prefix associated with the current node. |
| ProhibitDtd |
Obsolete.
Obsolete.
Gets or sets a value indicating whether to allow DTD processing. This property is obsolete. Use DtdProcessing instead. |
| QuoteChar |
Gets the quotation mark character used to enclose the value of an attribute node. |
| ReadState |
Gets the state of the reader. |
| SchemaInfo |
Gets the schema information that has been assigned to the current node as a result of schema validation. (Inherited from XmlReader) |
| Settings |
Gets the XmlReaderSettings object used to create this XmlTextReader instance. |
| Settings |
Gets the XmlReaderSettings object used to create this XmlReader instance. (Inherited from XmlReader) |
| Value |
Gets the text value of the current node. |
| ValueType |
Gets The Common Language Runtime (CLR) type for the current node. (Inherited from XmlReader) |
| WhitespaceHandling |
Gets or sets a value that specifies how white space is handled. |
| XmlLang |
Gets the current |
| XmlResolver |
Sets the XmlResolver used for resolving DTD references. |
| XmlSpace |
Gets the current |
Methods
| Name | Description |
|---|---|
| Close() |
Changes the ReadState to |
| Dispose() |
Releases all resources used by the current instance of the XmlReader class. (Inherited from XmlReader) |
| Dispose(Boolean) |
Releases the unmanaged resources used by the XmlReader and optionally releases the managed resources. (Inherited from XmlReader) |
| Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
| GetAttribute(Int32) |
Gets the value of the attribute with the specified index. |
| GetAttribute(String, String) |
Gets the value of the attribute with the specified local name and namespace URI. |
| GetAttribute(String) |
Gets the value of the attribute with the specified name. |
| GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
| GetNamespacesInScope(XmlNamespaceScope) |
Gets a collection that contains all namespaces currently in-scope. |
| GetRemainder() |
Gets the remainder of the buffered XML. |
| GetType() |
Gets the Type of the current instance. (Inherited from Object) |
| GetValueAsync() |
Asynchronously gets the value of the current node. (Inherited from XmlReader) |
| HasLineInfo() |
Gets a value indicating whether the class can return line information. |