
XmlReader and XmlReaderSettings Classes
Virtually all System.Xml components are built on top of the concept of parsing XML. For example, the XmlDocument class uses the XmlReader class to parse a document and build an in-memory representation of the XML document.
We recommend using the Create method for creating XmlReader objects. The XmlReaderSettings class specifies the set of features you want to enable on the XmlReader object.
Limiting Document and Entity Expansion Size
Memory usage of an application that uses XmlReader may have a correlation to the size of the parsed XML document. One form of denial of service attack is when excessively large XML documents are submitted to be parsed.
When using XmlReader, you can limit the size of the document that can be parsed by setting the MaxCharactersInDocument property. You can limit the number of characters that result from expanding entities by setting the MaxCharactersFromEntities property. See the appropriate reference topics for examples of setting these properties.
DTD Processing
DTD processing may lead to a DoS condition. For example, the DTD may contain nested entities or complex content models that can take an inordinate amount of time to process.
DTD processing is disabled by default. An XmlException is thrown when the XmlReader encounters DTD data.
Schema Processing
The ProcessInlineSchema and ProcessSchemaLocation validation flags of an XmlReaderSettings object are not set by default. This helps to protect the XmlReader against schema-based attacks when it is processing XML data from an untrusted source. When these flags are set, the XmlResolver of the XmlReaderSettings object is used to resolve schema locations encountered in the instance document in the XmlReader. If the XmlResolver property is set to null, schema locations are not resolved even if the ProcessInlineSchema and ProcessSchemaLocation validation flags are set.
Schemas added during validation add new types and can change the validation outcome of the document being validated. As a result, external schemas should only be resolved from trusted sources.
We recommend disabling the ProcessIdentityConstraints flag (enabled by default) when validating, untrusted, large XML documents in high availability scenarios against a schema with identity constraints over a large part of the document.
External Resources
XML data can include references to external resources such as a schema file. By default external resources are resolved using an XmlUrlResolver object with no user credentials. This means that, by default, you can access any locations that do not require credentials. You can secure this further by doing one of the following:
Sharing XmlReaderSettings Objects
Supporting Components
Data Processing
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.
You can create a custom IStream implementation that limits the size of input used and supply this to the XmlReader class.
Use the ReadValueChunk method 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.