XmlReader Class (System.Xml)

Switch View :
ScriptFree
.NET Framework Class Library
XmlReader Class

Represents a reader that provides fast, non-cached, forward-only access to XML data.

Inheritance Hierarchy

System.Object
  System.Xml.XmlReader
    System.Xml.XmlDictionaryReader
    System.Xml.XmlNodeReader
    System.Xml.XmlTextReader
    System.Xml.XmlValidatingReader

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Syntax

Visual Basic
Public MustInherit Class XmlReader _
	Implements IDisposable
C#
public abstract class XmlReader : IDisposable
Visual C++
public ref class XmlReader abstract : IDisposable
F#
[<AbstractClass>]
type XmlReader =  
    class
        interface IDisposable
    end

The XmlReader type exposes the following members.

Constructors

  Name Description
Protected method Supported by the XNA Framework Supported by Portable Class Library XmlReader Initializes a new instance of the XmlReader class.
Top
Properties

  Name Description
Public property Supported by the XNA Framework Supported by Portable Class Library AttributeCount When overridden in a derived class, gets the number of attributes on the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library BaseURI When overridden in a derived class, gets the base URI of the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library CanReadBinaryContent Gets a value indicating whether the XmlReader implements the binary content read methods.
Public property Supported by the XNA Framework Supported by Portable Class Library CanReadValueChunk Gets a value indicating whether the XmlReader implements the ReadValueChunk method.
Public property Supported by the XNA Framework Supported by Portable Class Library CanResolveEntity Gets a value indicating whether this reader can parse and resolve entities.
Public property Supported by the XNA Framework Supported by Portable Class Library Depth When overridden in a derived class, gets the depth of the current node in the XML document.
Public property Supported by the XNA Framework Supported by Portable Class Library EOF When overridden in a derived class, gets a value indicating whether the reader is positioned at the end of the stream.
Public property Supported by the XNA Framework Supported by Portable Class Library HasAttributes Gets a value indicating whether the current node has any attributes.
Public property Supported by the XNA Framework Supported by Portable Class Library HasValue When overridden in a derived class, gets a value indicating whether the current node can have a Value.
Public property Supported by the XNA Framework Supported by Portable Class Library IsDefault When overridden in a derived class, gets a value indicating whether the current node is an attribute that was generated from the default value defined in the DTD or schema.
Public property Supported by the XNA Framework Supported by Portable Class Library IsEmptyElement When overridden in a derived class, gets a value indicating whether the current node is an empty element (for example, <MyElement/>).
Public property Supported by the XNA Framework Supported by Portable Class Library Item[Int32] When overridden in a derived class, gets the value of the attribute with the specified index.
Public property Supported by the XNA Framework Supported by Portable Class Library Item[String] When overridden in a derived class, gets the value of the attribute with the specified Name.
Public property Supported by the XNA Framework Supported by Portable Class Library Item[String, String] When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI.
Public property Supported by the XNA Framework Supported by Portable Class Library LocalName When overridden in a derived class, gets the local name of the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library Name When overridden in a derived class, gets the qualified name of the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library NamespaceURI When overridden in a derived class, gets the namespace URI (as defined in the W3C Namespace specification) of the node on which the reader is positioned.
Public property Supported by the XNA Framework Supported by Portable Class Library NameTable When overridden in a derived class, gets the XmlNameTable associated with this implementation.
Public property Supported by the XNA Framework Supported by Portable Class Library NodeType When overridden in a derived class, gets the type of the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library Prefix When overridden in a derived class, gets the namespace prefix associated with the current node.
Public property Supported by the XNA Framework QuoteChar When overridden in a derived class, gets the quotation mark character used to enclose the value of an attribute node.
Public property Supported by the XNA Framework Supported by Portable Class Library ReadState When overridden in a derived class, gets the state of the reader.
Public property Supported by the XNA Framework SchemaInfo Gets the schema information that has been assigned to the current node as a result of schema validation.
Public property Supported by the XNA Framework Supported by Portable Class Library Settings Gets the XmlReaderSettings object used to create this XmlReader instance.
Public property Supported by the XNA Framework Supported by Portable Class Library Value When overridden in a derived class, gets the text value of the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library ValueType Gets The Common Language Runtime (CLR) type for the current node.
Public property Supported by the XNA Framework Supported by Portable Class Library XmlLang When overridden in a derived class, gets the current xml:lang scope.
Public property Supported by the XNA Framework Supported by Portable Class Library XmlSpace When overridden in a derived class, gets the current xml:space scope.
Top
Methods

  Name Description
Public method Supported by the XNA Framework Close When overridden in a derived class, changes the ReadState to Closed.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(Stream) Creates a new XmlReader instance using the specified stream.
Public method Static member Supported by the XNA Framework Create(String) Creates a new XmlReader instance with specified URI.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(TextReader) Creates a new XmlReader instance with the specified TextReader.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(Stream, XmlReaderSettings) Creates a new XmlReader instance with the specified stream and XmlReaderSettings object.
Public method Static member Supported by the XNA Framework Create(String, XmlReaderSettings) Creates a new instance with the specified URI and XmlReaderSettings.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(TextReader, XmlReaderSettings) Creates a new XmlReader instance using the specified TextReader and XmlReaderSettings objects.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(XmlReader, XmlReaderSettings) Creates a new XmlReader instance with the specified XmlReader and XmlReaderSettings objects.
Public method Static member Supported by the XNA Framework Create(Stream, XmlReaderSettings, String) Creates a new XmlReader instance using the specified stream, base URI, and XmlReaderSettings object.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(Stream, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance using the specified stream, XmlReaderSettings, and XmlParserContext objects.
Public method Static member Supported by the XNA Framework Create(String, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance using the specified URI, XmlReaderSettings, and XmlParserContext objects.
Public method Static member Supported by the XNA Framework Create(TextReader, XmlReaderSettings, String) Creates a new XmlReader instance using the specified TextReader, XmlReaderSettings, and base URI.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Create(TextReader, XmlReaderSettings, XmlParserContext) Creates a new XmlReader instance using the specified TextReader, XmlReaderSettings, and XmlParserContext objects.
Public method Supported by Portable Class Library Dispose()
Protected method Supported by the XNA Framework Supported by Portable Class Library Dispose(Boolean) Releases the unmanaged resources used by the XmlReader and optionally releases the managed resources.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetAttribute(Int32) When overridden in a derived class, gets the value of the attribute with the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library GetAttribute(String) When overridden in a derived class, gets the value of the attribute with the specified Name.
Public method Supported by the XNA Framework Supported by Portable Class Library GetAttribute(String, String) When overridden in a derived class, gets the value of the attribute with the specified LocalName and NamespaceURI.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsName Gets a value indicating whether the string argument is a valid XML name.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library IsNameToken Gets a value indicating whether or not the string argument is a valid XML name token.
Public method Supported by the XNA Framework Supported by Portable Class Library IsStartElement() Calls MoveToContent and tests if the current content node is a start tag or empty element tag.
Public method Supported by the XNA Framework Supported by Portable Class Library IsStartElement(String) Calls MoveToContent and tests if the current content node is a start tag or empty element tag and if the Name property of the element found matches the given argument.
Public method Supported by the XNA Framework Supported by Portable Class Library IsStartElement(String, String) Calls MoveToContent and tests if the current content node is a start tag or empty element tag and if the LocalName and NamespaceURI properties of the element found match the given strings.
Public method Supported by the XNA Framework Supported by Portable Class Library LookupNamespace When overridden in a derived class, resolves a namespace prefix in the current element's scope.
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToAttribute(Int32) When overridden in a derived class, moves to the attribute with the specified index.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToAttribute(String) When overridden in a derived class, moves to the attribute with the specified Name.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToAttribute(String, String) When overridden in a derived class, moves to the attribute with the specified LocalName and NamespaceURI.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToContent Checks whether the current node is a content (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity) node. If the node is not a content node, the reader skips ahead to the next content node or end of file. It skips over nodes of the following type: ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToElement When overridden in a derived class, moves to the element that contains the current attribute node.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToFirstAttribute When overridden in a derived class, moves to the first attribute.
Public method Supported by the XNA Framework Supported by Portable Class Library MoveToNextAttribute When overridden in a derived class, moves to the next attribute.
Public method Supported by the XNA Framework Supported by Portable Class Library Read When overridden in a derived class, reads the next node from the stream.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadAttributeValue When overridden in a derived class, parses the attribute value into one or more Text, EntityReference, or EndEntity nodes.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAs Reads the content as an object of the type specified.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsBase64 Reads the content and returns the Base64 decoded binary bytes.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsBinHex Reads the content and returns the BinHex decoded binary bytes.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsBoolean Reads the text content at the current position as a Boolean.
Public method Supported by the XNA Framework ReadContentAsDateTime Reads the text content at the current position as a DateTime object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsDecimal Reads the text content at the current position as a Decimal object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsDouble Reads the text content at the current position as a double-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsFloat Reads the text content at the current position as a single-precision floating point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsInt Reads the text content at the current position as a 32-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsLong Reads the text content at the current position as a 64-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsObject Reads the text content at the current position as an Object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadContentAsString Reads the text content at the current position as a String object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAs(Type, IXmlNamespaceResolver) Reads the element content as the requested type.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAs(Type, IXmlNamespaceResolver, String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the element content as the requested type.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsBase64 Reads the element and decodes the Base64 content.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsBinHex Reads the element and decodes the BinHex content.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsBoolean() Reads the current element and returns the contents as a Boolean object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsBoolean(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a Boolean object.
Public method Supported by the XNA Framework ReadElementContentAsDateTime() Reads the current element and returns the contents as a DateTime object.
Public method Supported by the XNA Framework ReadElementContentAsDateTime(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a DateTime object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsDecimal() Reads the current element and returns the contents as a Decimal object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsDecimal(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a Decimal object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsDouble() Reads the current element and returns the contents as a double-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsDouble(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a double-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsFloat() Reads the current element and returns the contents as single-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsFloat(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a single-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsInt() Reads the current element and returns the contents as a 32-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsInt(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a 32-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsLong() Reads the current element and returns the contents as a 64-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsLong(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a 64-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsObject() Reads the current element and returns the contents as an Object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsObject(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as an Object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsString() Reads the current element and returns the contents as a String object.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadElementContentAsString(String, String) Checks that the specified local name and namespace URI matches that of the current element, then reads the current element and returns the contents as a String object.
Public method Supported by the XNA Framework ReadElementString() Reads a text-only element.
Public method Supported by the XNA Framework ReadElementString(String) Checks that the Name property of the element found matches the given string before reading a text-only element.
Public method Supported by the XNA Framework ReadElementString(String, String) Checks that the LocalName and NamespaceURI properties of the element found matches the given strings before reading a text-only element.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadEndElement Checks that the current content node is an end tag and advances the reader to the next node.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadInnerXml When overridden in a derived class, reads all the content, including markup, as a string.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadOuterXml When overridden in a derived class, reads the content, including markup, representing this node and all its children.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadStartElement() Checks that the current node is an element and advances the reader to the next node.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadStartElement(String) Checks that the current content node is an element with the given Name and advances the reader to the next node.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadStartElement(String, String) Checks that the current content node is an element with the given LocalName and NamespaceURI and advances the reader to the next node.
Public method Supported by the XNA Framework ReadString When overridden in a derived class, reads the contents of an element or text node as a string.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadSubtree Returns a new XmlReader instance that can be used to read the current node, and all its descendants.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToDescendant(String) Advances the XmlReader to the next descendant element with the specified qualified name.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToDescendant(String, String) Advances the XmlReader to the next descendant element with the specified local name and namespace URI.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToFollowing(String) Reads until an element with the specified qualified name is found.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToFollowing(String, String) Reads until an element with the specified local name and namespace URI is found.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToNextSibling(String) Advances the XmlReader to the next sibling element with the specified qualified name.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadToNextSibling(String, String) Advances the XmlReader to the next sibling element with the specified local name and namespace URI.
Public method Supported by the XNA Framework Supported by Portable Class Library ReadValueChunk Reads large streams of text embedded in an XML document.
Public method Supported by the XNA Framework Supported by Portable Class Library ResolveEntity When overridden in a derived class, resolves the entity reference for EntityReference nodes.
Public method Supported by the XNA Framework Supported by Portable Class Library Skip Skips the children of the current node.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString Returns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method Supported by the XNA Framework IDisposable.Dispose For a description of this member, see IDisposable.Dispose.
Top
Remarks

XmlReader provides forward-only, read-only access to a stream of XML data. The XmlReader class conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.

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.

Important note Important

Although the Microsoft .NET Framework includes concrete implementations of the XmlReader class, such as the XmlTextReader, XmlNodeReader, and the XmlValidatingReader classes, in the 2.0 release the recommended practice is to create XmlReader instances using the Create method. For more information, see Creating XML Readers.

XmlReader 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 the reader is in error state.

For further discussion on the XmlReader class, see Reading XML with the XmlReader.

Security Considerations

The following items are things to consider when working with the XmlReader class.

  • Exceptions thrown the XmlReader can disclose path information that you do not want bubbled up to the application. Your applications must catch exceptions and process them appropriately.

  • Do not enable DTD processing if you are concerned about denial of service issues or if you are dealing with untrusted sources. DTD processing is disabled by default for XmlReader objects created by the Create method.

    If you have DTD processing enabled, you can use the XmlSecureResolver to restrict the resources that the XmlReader 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 schema 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:

  • 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 XmlReader, create a custom IStream implementation and supply it the XmlReader.

  • 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.

  • When reading an XML document with a large number of unique local names, namespaces, or prefixes, a problem can occur. If you are using a class that derives from XmlReader, and you call either the LocalName, Prefix, or NamespaceURI property for each item, the returned string is added to a NameTable. The collection held by the NameTable never decreases in size, creating a virtual "memory leak" of the string handles. One mitigation for this is to derive from the NameTable class and enforce a maximum size quota. (There is no way to prevent the use of a NameTable, or to switch the NameTable when it is full). Another mitigation is to avoid using the properties mentioned and instead use the MoveToAttribute method with the IsStartElement method where possible; those methods do not return strings and thus avoid the problem of overfilling the NameTable collection.

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Other Resources

Community Content

masbaaz
Disposing XmlReader object

you can explicitly cast the  XMLReader object to System.IDisposable inorder to gain access to Dispose() method.

For example:

suppose we have belowScenario:



            XmlReader reader = XmlReader.Create("ServiceReferences.ClientConfig");

            reader.MoveToContent();

            while (reader.Read())

            { 

                // DO your logic and processing

            }



//Then proceed to dispose as below:

            ((System.IDisposable)(reader)).Dispose();

((System.IDisposable)(reader)).Dispose();


This is how you can dispose  XmlReader object by explicityly casting to (System.IDisposable)


HerbF
Wrap inside a using statement to dispose
You can also wrap it within a using statement to dispose of it.  No need to cast in this case.

webJose
The parameterless Dispose() method is not public
I guess htis is a documentation bug. The parameterless Dispose() method is not public.
Incorrect. Dispose() is public, but you must read carefully: The interface implementation is explicit. You must explicitly cast the object to IDisposable in order to gain access to Dispose().

webJose
Dispoe() is not public
I guess the documentation has a bug the no parameter Dispose() method on XmlReader is not public.

Incorrect. Dispose() is public, but you must read carefully: The interface implementation is explicit. You must explicitly cast the object to IDisposable in order to gain access to Dispose().

Jamie A. Hankins
Dispose...
Nothing stops you from casting it to IDisposable and calling Dispose, right?