New Features in the XmlReader Class
The Microsoft .NET Framework version 2.0 includes many design and functionality changes to the XmlReader, class.
XmlReader Creation
In this release, the Create method is the preferred mechanism for obtaining XmlReader instances. The Create method uses the XmlReaderSettings class to specify which features the XmlReader instance should support.
For more information, see Creating XML Readers.
Data Validation
XmlReader objects created by the Create method can enforce validation using a document type definition (DTD) or Schema definition language (XSD) schema. The XmlReaderSettings.ValidationType property determines whether the XmlReader instance enforces validation. The XmlReaderSettings.ValidationFlags property configures optional validation settings. The XmlSchemaSet class is used to cache XML Schemas.
Note
The XmlValidatingReader and XmlSchemaCollection classes are obsolete in the .NET Framework 2.0.
For more information, see Validating XML Data with XmlReader.
Data Conformance
XmlReader objects created by the Create method are, by default, more conformant than the XmlTextReader implementation. XmlReader objects created by the Create method support the following features by default:
Normalize new line characters.
Expand entities.
Add default attributes.
The XmlReaderSettings.CheckCharacters and XmlReaderSettings.ConformanceLevel properties allow you to specify the type of conformance checks you want to enable on the created XmlReader object. For more information, see Data Conformance Checking with XmlReader.
Type Support
Provides support for retrieving XML Schema definition language (XSD) schema information and permits callers to request values as simple-typed common language runtime (CLR) values.
The ReadContentAs and ReadElementContentAs methods can read content as system types rather than strings. These new methods allow users to get values in the representation that is most appropriate for the coding job without having to manually perform value conversions and parsing.
For more information, see Reading Typed Data.
New Helper Methods
The XmlReader class includes new methods that should make it easier to parse XML data:
ReadSubtree – Returns a new XmlReader that reads the current element and all of its descendants.
ReadToDescendant – Advances the reader to the next child element that matches the specified name.
ReadToNextSibling - Advances the reader to the next sibling element that matches the specified name.
ReadToFollowing - Advances the reader to the next following element that matches the specified name.
ReadValueChunk - Reads large streams of text embedded in an XML document in a streaming fashion.
ReadContentAsBase64 and ReadElementContentAsBase64 - Reads large streams of binary data embedded in an XML document in a streaming fashion and decodes the Base64 content.
ReadContentAsBinHex and ReadElementContentAsBinHex - Reads large streams of binary data embedded in an XML document in a streaming fashion and decodes the BinHex content.
Security Features
You can create an XmlReader object that prohibits document type definition (DTD) processing. Disabling DTD processing can be useful in preventing certain denial of service attacks. When DTD processing is disabled, the XmlReader object throws an XmlException when any DTD content is encountered. By default, DTD processing is disabled. To enable DTD processing, set the XmlReaderSettings.ProhibitDtd property to false before creating the XmlReader.
Note
The ProhibitDtd property is also available on the XmlTextReader class.
Entity Handling
XmlReader objects created by the Create method expand all entities automatically. An exception is thrown if entities are encountered that cannot be resolved.