Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Reading XML with the XmlReader

Reading XML with the XmlReader

.NET Framework 1.1

The XmlReader class is an abstract base class that provides non-cached, forward-only, read-only access. The XmlReader class checks that the XML is well-formed, and throws XmlExceptions if an error is encountered. It can read a stream or a document, and it implements the namespace requirements outlined in the recommendation provided by the W3C located at www.w3.org/TR/REC-xml-names.

Being an abstract base class enables you to customize your own type of reader or extend the current implementations of XmlTextReader, XmlValidatingReader, and XmlNodeReader classes. However, security constraints implemented in the .NET Framework version 1.1 on the XmlReader limit the ability to inherit the XmlTextReader and XmlValidatingReader. In the .NET Framework version 1.0, any component can inherit from the XmlTextReader or XmlValidatingReader. By implementing an inheritance demand on the constructor of the XmlTextReader and XmlValidatingReader in .NET Framework version 1.1, trusted components are now the only components with the ability to inherit from the XmlTextReader or XmlValidatingReader.

The XmlReader class defines methods that enable you to pull data from XML or to skip unwanted records. XmlReader is unlike the SAX model, which is a push model with the parser pushing events to the application. For more information on a comparison to SAX, see Comparing XmlReader to SAX Model.

The XmlReader class has methods to:

  • Read XML content when the content is available in its entirety, for example, an XML text file.
  • Find the depth of the XML element stack.
  • Determine if an element has content or is empty.
  • Read and navigate attributes.
  • Skip over elements and their content.

The XmlReader class has properties that return information, such as:

  • The type and name of the current node.
  • The content of the current node.

Implementations of the XmlReader class extend the base class and vary in their design to support different scenario needs. The following table describes the implementations of the XmlReader class.

ClassDescription
XmlTextReader ClassReads character streams. It is a forward-only reader that has methods that return data on content and node types. There is no Document Type Definition (DTD) or schema support.
XmlNodeReader ClassProvides a parser over an XML Document Object Model (DOM) API, like the XmlNode tree. Takes in an XmlNode, it returns whatever nodes it finds in the DOM tree, including entity reference nodes. There is no DTD or schema validation support but it can resolve entities defined in DTD.
XmlValidatingReader ClassProvides a fully compliant validating or non-validating XML parser with DTD, XML Schema definition language (XSD) schema or XML-Data Reduced (XDR) schema support. Takes an XmlTextReader and layers validation services on top.
Customized XML Reader CreationAllows developer-defined derivations of the XmlReader.
Note   The XmlTextReader and XmlValidatingReader are constrained on the size of files they can read. They cannot read files larger than 2 gigabytes. If it is possible, split the source file into smaller, multiple files.

The XsltReader is another implementation that does not have a public constructor. It is created as a result of calling the Transform method on the XslTransform class. The XsltReader provides the following functionality:

  • Enforces the rules that XML must be well-formed.
  • Does not validate against DTDs or schemas.
  • Does not expand default attributes, because DTD information (DOCTYPE nodes) are not exposed in the XPath Data Model. See the XmlReader.IsDefault property. If you need to expand default attributes in the source document before applying a transformation, then the data store (for example, XmlDocument) needs to be loaded through an XmlValidatingReader.

Each of the classes, XmlTextReader, XmlValidatingReader, and XmlNodeReader, are designed to support different scenarios. The table below describes which reader to use for each scenario, and what value to give the ValidationType property.

ScenarioReader to useValidationType property
Needs greater performance and does not need any DTD or Schema support.XmlTextReaderNot available.
Requires the XML to be well-formed, including external entities and DocTypes with a supplied DTD.XmlTextReaderNot available.
Needs XML to be valid and well-formed according to the DTD.XmlValidatingReaderAuto or DTD.
Needs XML to be well-formed and validated against a schema.XmlValidatingReaderAuto when no DTD is available, or XDR schema.
Needs XML to be well-formed when streaming XML data from an XmlNode.XmlNodeReaderNot available.

The following table describes how the XmlResolver and XmlTextReader.Normalization properties on the various readers can be set to achieve each scenario.

The Normalization property, when set to true, normalizes the end-of-line characters in text and white space nodes, and also normalizes the attribute values according to their type. As a result, setting the Normalization property to true results in slower performance than when it is set to false, which is the default value. For more information, see XmlTextReader.Normalization Property.

The XmlResolver property is used for resolving external resources named by a Uri, such as an external DTD or locating a schema. For more information on using the XmlResolver property with the different readers, see Resolving Resources Using the XmlResolver.

ScenarioXmlResolverNormalization property
Needs greater performance and does not need any DTD or Schema support.Set to null reference. For more information, see XmlTextReader.XmlResolver Property.Set to false.
Requires document to be well-formed, including external entities and DocTypes with a supplied DTD.Set to non-null reference. All external entities must be resolvable. For more information, see XmlTextReader.XmlResolver Property.Set to true.
Needs document to be well-formed, and needs the XML to be valid according to the DTD.Set to non-null reference. All external entities must be resolvable. For more information, see XmlValidatingReader.XmlResolver Property.Set to true on XmlTextReader before being passed to XmlValidatingReader.
Needs document to be well-formed and needs schema validation.Set to non-null reference. All external entities must be resolvable. For more information, see XmlValidatingReader.XmlResolver Property.Set to true on XmlTextReader before being passed to XmlValidatingReader.
Needs document to be well-formed when streaming XML data from an XmlNode.Not available.Not available.

The XmlValidatingReader provides validation services over an XmlTextReader. For more information, see Validation of XML with XmlValidatingReader.

See Also

Current Node Position in XmlReader | Property Settings on XmlReader | Object Comparison Using XmlNameTable with XmlReader | Reading Attributes with XmlReader | Reading Element and Attributes Content | Skipping Content with XmlReader | EntityReference Reading and Expansion | Comparing XmlReader to SAX Reader | Reading XML Data with XmlTextReader | Reading Node Trees with XmlNodeReader | Validating XML with XmlValidatingReader | Customized XML Reader Creation | XmlReader Class | XmlReader Members | XmlNodeReader Class | XmlNodeReader Members | XmlTextReader Class | XmlTextReader Members | XmlValidatingReader Class | XmlValidatingReader Members

Show:
© 2015 Microsoft