public class XmlTextReader : XmlReader
Object
XmlReader
XmlTextReader
System.Xml
XML
Represents a reader that provides fast, non-cached, forward-only access to XML data.
This class provides forward-only, read-only access to a character stream of XML data. This class enforces the rules of well-formed XML but does not perform data validation.This class implements the XmlReader class and conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.
A given set of XML data is modeled as a tree of nodes. The different types of nodes are specified in the XmlNodeType enumeration. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the "read" or "moveto" methods. The following table lists the node properties exposed for the current node.
This class does not expand default attributes or resolve general entities. Any general entities encountered are returned as a single empty
Property Description AttributeCount The number of attributes on the node. BaseUri The base URI of the node. Depth The depth of the node in the tree. HasAttributes Whether the node has attributes. (Inherited from XmlReader) HasValue Whether the node can have a text value. IsDefault Whether an Attribute
node was generated from the default value defined in the DTD or schema.IsEmptyElement Whether an Element
node is empty.LocalName The local name of the node. Name The qualified name of the node, equal to Prefix
:LocalName
.NamespaceUri The URI defining the namespace associated with the node. NodeType The XmlNodeType of the node. Prefix A shorthand reference to the namespace associated with the node. QuoteChar The quotation mark character used to enclose the value of an attribute. Value The text value of the node. XmlLang The xml:lang
scope within which the node resides.EntityReference
node.This class checks that a Document Type Definition (DTD) is well-formed, but does not validate using the DTD.
To read strongly typed data, use the XmlConvert class.
This class throws a 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.
DefaultMemberAttribute("Item")
System.Xml Namespace
XmlTextReader Constructors
XmlTextReader() Constructor
XmlTextReader(System.Xml.XmlNameTable) Constructor
XmlTextReader(System.IO.Stream) Constructor
XmlTextReader(System.String, System.IO.Stream) Constructor
XmlTextReader(System.IO.Stream, System.Xml.XmlNameTable) Constructor
XmlTextReader(System.String, System.IO.Stream, System.Xml.XmlNameTable) Constructor
XmlTextReader(System.IO.TextReader) Constructor
XmlTextReader(System.String, System.IO.TextReader) Constructor
XmlTextReader(System.IO.TextReader, System.Xml.XmlNameTable) Constructor
XmlTextReader(System.String, System.IO.TextReader, System.Xml.XmlNameTable) Constructor
XmlTextReader(System.IO.Stream, System.Xml.XmlNodeType, System.Xml.XmlParserContext) Constructor
XmlTextReader(System.String, System.Xml.XmlNodeType, System.Xml.XmlParserContext) Constructor
XmlTextReader(System.String) Constructor
XmlTextReader(System.String, System.Xml.XmlNameTable) Constructor
XmlTextReader Methods
XmlTextReader.Close Method
XmlTextReader.GetAttribute(int) Method
XmlTextReader.GetAttribute(System.String, System.String) Method
XmlTextReader.GetAttribute(System.String) Method
XmlTextReader.GetRemainder Method
XmlTextReader.LookupNamespace Method
XmlTextReader.MoveToAttribute(int) Method
XmlTextReader.MoveToAttribute(System.String, System.String) Method
XmlTextReader.MoveToAttribute(System.String) Method
XmlTextReader.MoveToElement Method
XmlTextReader.MoveToFirstAttribute Method
XmlTextReader.MoveToNextAttribute Method
XmlTextReader.Read Method
XmlTextReader.ReadAttributeValue Method
XmlTextReader.ReadBase64 Method
XmlTextReader.ReadBinHex Method
XmlTextReader.ReadChars Method
XmlTextReader.ReadInnerXml Method
XmlTextReader.ReadOuterXml Method
XmlTextReader.ReadString Method
XmlTextReader.ResetState Method
XmlTextReader.ResolveEntity Method
XmlTextReader Properties
XmlTextReader.AttributeCount Property
XmlTextReader.BaseURI Property
XmlTextReader.Depth Property
XmlTextReader.EOF Property
XmlTextReader.Encoding Property
XmlTextReader.HasValue Property
XmlTextReader.IsDefault Property
XmlTextReader.IsEmptyElement Property
XmlTextReader.Item(int) Property
XmlTextReader.Item(System.String) Property
XmlTextReader.Item(System.String, System.String) Property
XmlTextReader.LineNumber Property
XmlTextReader.LinePosition Property
XmlTextReader.LocalName Property
XmlTextReader.Name Property
XmlTextReader.NameTable Property
XmlTextReader.NamespaceURI Property
XmlTextReader.Namespaces Property
XmlTextReader.NodeType Property
XmlTextReader.Normalization Property
XmlTextReader.Prefix Property
XmlTextReader.QuoteChar Property
XmlTextReader.ReadState Property
XmlTextReader.Value Property
XmlTextReader.WhitespaceHandling Property
XmlTextReader.XmlLang Property
XmlTextReader.XmlResolver Property
XmlTextReader.XmlSpace Property
protected XmlTextReader();
Constructs a new instance of the XmlTextReader class.
System.Xml.XmlTextReader Class, System.Xml Namespace
protected XmlTextReader(XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified name table.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException nt is null
.
The XmlTextReader public constructors call this constructor to initialize the following XmlTextReader properties to the specified values. Derived classes can call this constructor to incorporate this behavior.
Property Value Namespaces true
NameTable nt Normalization false
ReadState System.Xml.ReadState.Initial WhitespaceHandling System.Xml.WhitespaceHandling.All XmlLang System.String.Empty XmlSpace System.Xml.XmlSpace.None XmlResolver new XmlUrlResolver()
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(Stream input);
Constructs and initializes a new instance of the XmlTextReader class with the specified stream.
- input
- The Stream containing the XML data to read.
Exception Type Condition ArgumentNullException input is null
.
This constructor is equivalent to XmlTextReader(System.String.Empty, input, new NameTable()).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url, Stream input);
Constructs and initializes a new instance of the XmlTextReader class with the specified URL and stream.
- url
- A String specifying the URL to use for resolving external resources.
- input
- The Stream containing the XML data to read.
Exception Type Condition ArgumentNullException input is null
.
This constructor is equivalent to XmlTextReader(url, input, new NameTable()).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(Stream input, XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified stream and name table.
- input
- The Stream containing the XML data to read.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException input or nt is null
.
This constructor is equivalent to XmlTextReader(System.String.Empty, input, nt).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url, Stream input, XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified URL, stream, and name table.
- url
- A String specifying the URL to use for resolving external resources.
- input
- The Stream containing the XML data to read.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException input or nt is null
.
This constructor calls XmlTextReader(nt) to initialize properties of the class.System.Xml.XmlTextReader.Encoding is set to the encoding corresponding to the byte-order mark at the beginning of the stream or, if no byte-order mark is found, UTF-8.
System.Xml.XmlTextReader.BaseURI is set to url or, if url is
null
, to System.String.Empty.
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(TextReader input);
Constructs and initializes a new instance of the XmlTextReader class with the specified TextReader.
- input
- A TextReader, set to the correct encoding, containing the XML data to read.
This constructor is equivalent to XmlTextReader(System.String.Empty, input, new NameTable()).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url, TextReader input);
Constructs and initializes a new instance of the XmlTextReader class with the specified URL and TextReader.
- url
- A String specifying the URL to use for resolving external resources.
- input
- A TextReader, set to the correct encoding, containing the XML data to read.
This constructor is equivalent to XmlTextReader(url, input, new NameTable()).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(TextReader input, XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified TextReader, and name table.
- input
- A TextReader, set to the correct encoding, containing the XML data to read.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException nt is null
.
This constructor is equivalent to XmlTextReader(System.String.Empty, input, nt).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url, TextReader input, XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified URL, TextReader, and name table.
- url
- A String specifying the URL to use for resolving external resources.
- input
- A TextReader, set to the correct encoding, containing the XML data to read.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException nt is null
.
If input isnull
, a XmlException is thrown when the System.Xml.XmlTextReader.Read method is called.This constructor calls XmlTextReader(nt) to initialize properties of the class.
System.Xml.XmlTextReader.BaseURI is set to url or, if url is
null
, to System.String.Empty.[Note: To pass a user defined string that represents full, well-formed XML data, create a StringReader with the string and pass the StringReader to this constructor.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(Stream xmlFragment, XmlNodeType fragType, XmlParserContext context);
Constructs and initializes a new instance of the XmlTextReader class with the specified stream containing an XML fragment.
- xmlFragment
- The Stream containing the XML fragment to parse.
- fragType
- The XmlNodeType of the XML fragment. This also determines what the fragment string can contain. (See table below.)
- context
- The XmlParserContext in which the xmlFragment is to be parsed, or
null
.
Exception Type Condition ArgumentNullException xmlFragment is null
.XmlException fragType is not an Element
,Attribute
, orDocument
XmlNodeType.
The following table lists valid values for fragType.
[Note: If the XML fragment is an element or attribute, the root level rules for well-formed XML documents are not enforced.
XmlNodeType Fragment May Contain Element Any valid element content (for example, any combination of elements, comments, processing instructions, CDATA sections, text, and entity references). Attribute The value of an attribute (the part inside the quotes). Document The contents of an entire XML document; document level rules are enforced. ]
This constructor calls XmlTextReader(context.
NameTable
) or, if context isnull
, XmlTextReader(new
NameTable()) to initialize properties of the class. Afterwards, the following XmlTextReader properties are set to the specified values.
Property Value BaseUri context. BaseURI
or, if context isnull
, System.String.Empty.Encoding context. Encoding
or, if context or context.Encoding
isnull
, the encoding corresponding to the byte-order mark at the beginning of the stream or, if no byte-order mark is found, UTF-8.Namespaces false
.XmlLang If context is not null
, context.XmlLang
. If context isnull
, this property is not changed.XmlSpace If context is not null
, context.XmlSpace
. If context isnull
, this property is not changed.
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string xmlFragment, XmlNodeType fragType, XmlParserContext context);
Constructs and initializes a new instance of the XmlTextReader class with the specified XML fragment.
- xmlFragment
- A String containing the XML fragment to parse.
- fragType
- The XmlNodeType of the XML fragment. This also determines what the fragment string can contain. (See table below.)
- context
- The XmlParserContext in which the xmlFragment is to be parsed, or
null
.
Exception Type Condition ArgumentNullException xmlFragment is null
.XmlException fragType is not an Element
,Attribute
, orDocument
XmlNodeType.
The following table lists valid values for fragType and how the reader will parse each of the different node types.
[Note: If the XML fragment is an element or attribute, root level rules for well-formed XML documents are not enforced.
XmlNodeType Fragment May Contain Element Any valid element content (for example, any combination of elements, comments, processing instructions, CDATA sections, text, and entity references). Attribute The value of an attribute (the part inside the quotes). Document The contents of an entire XML document; document level rules are enforced. This constructor can handle strings returned from System.Xml.XmlTextReader.ReadInnerXml.
]
This constructor calls XmlTextReader(context.
NameTable
) or, if context isnull
, XmlTextReader(new
NameTable()) to initialize properties of the class. Following this call, System.Xml.XmlTextReader.Namespaces is set tofalse
and, if context is notnull
, the following XmlTextReader properties are set to the specified values.
Property Value BaseUri context. BaseURI
or, if context isnull
, System.String.Empty.Encoding context. Encoding
or, if context or context.Encoding
isnull
, UTF-8.Namespaces false
.XmlLang If context is not null
, context.XmlLang
. If context isnull
, this property is not changed.XmlSpace If context is not null
, context.XmlSpace
. If context isnull
, this property is not changed.
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url);
Constructs and initializes a new instance of the XmlTextReader class with the specified file.
- url
- A String specifying the URL for the file containing the XML data.
Exception Type Condition XmlException url is null
.
This constructor is equivalent to XmlTextReader(url, new NameTable()).
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlTextReader(string url, XmlNameTable nt);
Constructs and initializes a new instance of the XmlTextReader class with the specified file and name table.
- url
- A String specifying the URL for the file containing the XML data to read.
- nt
- The XmlNameTable to use.
Exception Type Condition ArgumentNullException nt is null
.XmlException url is null
.
This constructor calls XmlTextReader(nt) to initialize properties of the class.
System.Xml.XmlTextReader Class, System.Xml Namespace
public override void Close();
Changes the System.Xml.XmlTextReader.ReadState toClosed
.
This method releases any resources allocated by the current instance, changes the System.Xml.XmlTextReader.ReadState to System.Xml.ReadState.Closed, and calls theClose
method of any underlying Stream or TextReader instance.[Note: This method overrides System.Xml.XmlReader.Close.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string GetAttribute(int i);
Returns the value of the attribute with the specified index relative to the containing element.
- i
- A Int32 specifying the zero-based index of the attribute relative to the containing element.
A String containing the value of the specified attribute.
Exception Type Condition ArgumentOutOfRangeException i is less than 0, or greater than or equal to the System.Xml.XmlTextReader.AttributeCount of the containing element.[Note: System.Xml.XmlTextReader.AttributeCount returns zero for all node types except Attribute
,DocumentType
,Element
, andXmlDeclaration
. Therefore, this exception is thrown if the reader is not positioned on one of these node types.]
This method does not move the reader.[Note: This method overrides System.Xml.XmlReader.GetAttribute(System.String). ]
See the System.Xml.XmlTextReader.GetAttribute(System.Int32)(String
,String
) method for an example using all three overloads of this method.
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string GetAttribute(string localName, string namespaceURI);
Returns the value of the attribute with the specified local name and namespace URI.
- localName
- A String specifying the local name of the attribute.
- namespaceURI
- A String specifying the namespace URI of the attribute.
A String containing the value of the specified attribute, ornull
if the attribute is not found. If localname isnull
,null
is returned.
If namespaceURI isnull
, the local namespace is searched for localName.This method does not move the reader.
[Note: This method overrides System.Xml.XmlReader.GetAttribute(System.String).
]
This example writes the value of the attributes from the following XML fragment to the console:
<test xmlns:dt="urn:datatypes" dt:type="int"/>
The second attribute value is retrieved using all three overloads of this method.
using System; using System.Xml; public class Reader { public static void Main() { string xmlFragment = @"<test xmlns:dt=""urn:datatypes"" dt:type=""int""/>"; NameTable nameTable = new NameTable(); XmlNamespaceManager xmlNsMan = new XmlNamespaceManager(nameTable); XmlParserContext xmlPContext = new XmlParserContext(null, xmlNsMan, null, XmlSpace.None); XmlTextReader xmlTReader = new XmlTextReader(xmlFragment,XmlNodeType.Element, xmlPContext); xmlTReader.Read(); Console.WriteLine( "{0}", xmlTReader.GetAttribute(0) ); string str1 = xmlTReader.GetAttribute(1); string str2 = xmlTReader.GetAttribute("dt:type"); string str3 = xmlTReader.GetAttribute("type", "urn:datatypes"); Console.WriteLine("{0} - {1} - {2}", str1, str2, str3); } }The output isurn:datatypes
int - int - int
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string GetAttribute(string name);
Returns the value of the attribute with the specified qualified name.
- name
- A String specifying the qualified name of the attribute.
A String containing the value of the specified attribute, ornull
if the attribute is not found. If name isnull
,null
is returned.
This method does not move the reader.[Note: If the reader is positioned on a
DocumentType
node, this method can be used to get the PUBLIC and SYSTEM literals.This method overrides System.Xml.XmlReader.GetAttribute(System.String).
]
See the System.Xml.XmlTextReader.GetAttribute(System.Int32)(String
,String
) method for an example using all three overloads of this method.
System.Xml.XmlTextReader Class, System.Xml Namespace
public TextReader GetRemainder();
Returns the remainder of the buffered XML.
The StringReader attached to the XML.
This method calls the System.Xml.XmlTextReader.Close method, and then resets the System.Xml.XmlTextReader.ReadState to System.Xml.ReadState.EndOfFile.[Note: Because XmlTextReader performs a buffered read operation, it must be able to return the remainder of the unused buffer so that no data is lost. For example, this allows protocols (such as multi-part MIME) to package XML in the same stream.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string LookupNamespace(string prefix);
Resolves a namespace prefix in the scope of the current element.
- prefix
- A String specifying the prefix whose namespace URI is to be resolved. To return the default namespace, specify System.String.Empty.
A String containing the namespace URI to which the prefix maps. If System.Xml.XmlTextReader.Namespaces isfalse
, prefix is not in System.Xml.XmlTextReader.NameTable, or no matching namespace is found,null
is returned.
Exception Type Condition ArgumentNullException The System.Xml.XmlTextReader.Namespaces property of the current instance is true
and prefix isnull
.
[Note: In the following XML, if the reader is positioned on thehref
attribute, the prefix "a" is resolved by calling System.Xml.XmlTextReader.LookupNamespace(System.String)("a"). The returned string is "urn:456".
<root xmlns:a="urn:456">
This method overrides System.Xml.XmlReader.LookupNamespace(System.String).<item>
<ref href="a:b"/>
</item>
</root>
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override void MoveToAttribute(int i);
Moves the position of the current instance to the attribute with the specified index relative to the containing element.
- i
- A Int32 specifying the zero-based index of the attribute relative to the containing element.
Exception Type Condition ArgumentOutOfRangeException i is less than 0 or greater than or equal to the System.Xml.XmlTextReader.AttributeCount of the containing element.[Note: System.Xml.XmlTextReader.AttributeCount returns zero for all node types except Attribute
,DocumentType
,Element
, andXmlDeclaration
. Therefore, this exception is thrown if the reader is not positioned on one of these node types.]
After calling this method, the System.Xml.XmlTextReader.Name, System.Xml.XmlTextReader.NamespaceURI, and System.Xml.XmlTextReader.Prefix properties reflect the properties of the new attribute.[Note: This method overrides System.Xml.XmlReader.MoveToAttribute(System.String).
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool MoveToAttribute(string localName, string namespaceURI);
Moves the position of the current instance to the attribute with the specified local name and namespace URI.
- localName
- A String specifying the local name of the attribute.
- namespaceURI
- A String specifying the namespace URI of the attribute.
A Boolean wheretrue
indicates the attribute was found. If localname isnull
, or the attribute was not found,false
is returned and the position of the reader does not change.
If namespaceURI isnull
, the local namespace is searched for localName.After calling this method, the System.Xml.XmlTextReader.Name, System.Xml.XmlTextReader.NamespaceURI, and System.Xml.XmlTextReader.Prefix properties reflect the properties of the new attribute.
[Note: This method overrides System.Xml.XmlReader.MoveToAttribute(System.String).
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool MoveToAttribute(string name);
Moves the position of the current instance to the attribute with the specified qualified name.
- name
- A String specifying the qualified name of the attribute.
A Boolean wheretrue
indicates the attribute was found. If name isnull
, or the attribute was not found,false
is returned and the position of the reader does not change.
After calling this method, the System.Xml.XmlTextReader.Name, System.Xml.XmlTextReader.NamespaceURI, and System.Xml.XmlTextReader.Prefix properties reflect the properties of the new attribute.[Note: This method overrides System.Xml.XmlReader.MoveToAttribute(System.String).
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool MoveToElement();
Moves the position of the current instance to the node that contains the currentAttribute
node.
A Boolean wheretrue
indicates the reader was moved;false
indicates the reader was not positioned on anAttribute
node and therefore was not moved.
[Note: TheDocumentType
,Element
, andXmlDeclaration
node types can contain attributes.This method overrides System.Xml.XmlReader.MoveToElement.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool MoveToFirstAttribute();
Moves the position of the current instance to the first attribute associated with the current node.
A Boolean wheretrue
indicates the current node contains at least one attribute; otherwise,false
.
If System.Xml.XmlTextReader.AttributeCount is non-zero, the reader moves to the first attribute; otherwise, the position of the reader does not change.[Note: This method overrides System.Xml.XmlReader.MoveToFirstAttribute.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool MoveToNextAttribute();
Moves the position of the current instance to the next attribute associated with the current node.
A Boolean wheretrue
indicates the reader moved to the next attribute;false
if there were no more attributes.
If the current node is an element node, this method is equivalent to System.Xml.XmlTextReader.MoveToFirstAttribute.[Note: This method overrides System.Xml.XmlReader.MoveToNextAttribute.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool Read();
Moves the position of the current instance to the next node in the stream, exposing its properties.
A Boolean wheretrue
indicates the node was read successfully, andfalse
indicates there were no more nodes to read.
Exception Type Condition XmlException An error occurred while parsing the XML.
[Note: When a reader is first created and initialized, there is no information available. Calling this method is required to read the first node.This method overrides System.Xml.XmlReader.Read.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool ReadAttributeValue();
Parses an attribute value into one or moreText
andEntityReference
nodes.
A Boolean wheretrue
indicates the attribute value was parsed, andfalse
indicates the reader was not positioned on an attribute node or all the attribute values have been read.
The XmlTextReader class does not expand general entities; any encountered are returned as a single emptyEntityReference
node (System.Xml.XmlTextReader.Value is System.String.Empty).[Note: Use this method after calling System.Xml.XmlTextReader.MoveToAttribute(System.String) to read through the text or entity reference nodes that make up the attribute value. The System.Xml.XmlTextReader.Depth of the attribute value nodes is one plus the depth of the attribute node. When general entity references are stepped into or out of, the System.Xml.XmlTextReader.Depth is incremented of decremented by one, respectively.
This method overrides System.Xml.XmlReader.ReadAttributeValue.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public int ReadBase64(byte[] array, int offset, int len);
Reads and decodes the Base64 encoded contents of an element and stores the result in a byte buffer.
- array
- A Byte array to store the content.
- offset
- A Int32 specifying the zero-based index into array where the method should begin to write.
- len
- A Int32 specifying the number of bytes to write.
A Int32 containing the number of bytes written to array, or zero if the current instance is not positioned on an element.
Exception Type Condition ArgumentNullException array is null
.ArgumentOutOfRangeException offset < 0, or len < 0. - or -
len > array.
Length
- offset.
XmlException The Base64 sequence is not valid.
[Note: This method can be called successively to read large streams of embedded text.Base64 encoding represents byte sequences in a text form comprised of the 65 US-ASCII characters (A-Z, a-z, 0-9, +, /, =) where each character encodes 6 bits of the binary data.
For more information on Base64 encoding, see RFC 2045 (http://www.ietf.org/rfc/2045).
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public int ReadBinHex(byte[] array, int offset, int len);
Reads and decodes the BinHex encoded contents of an element and stores the result in a byte buffer.
- array
- A Byte array to store the content.
- offset
- A Int32 specifying the zero-based index into array where the method should begin to write.
- len
- A Int32 specifying the number of bytes to write.
A Int32 containing the number of bytes written to array, or zero if the current instance is not positioned on an element.
Exception Type Condition ArgumentNullException array is null
.ArgumentOutOfRangeException offset < 0, or len < 0. The Base64 sequence is not valid. - or -
len > array.
Length
- offset.
XmlException The BinHex sequence is not valid.
[Note: This method can be called successively to read large streams of embedded text.For information on BinHex encoding, see RFC 1741 (http://www.ietf.org/rfc/rfc1741).
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public int ReadChars(char[] buffer, int index, int count);
Reads the text contents of an element into a character buffer.
- buffer
- A Char array to store the content.
- index
- A Int32 specifying the zero-based index into buffer where the method should begin to write.
- count
- A Int32 specifying the number of characters to read and store in buffer.
A Int32 containing the number of characters written to buffer, or zero if the current instance is not positioned on an element.
Exception Type Condition ArgumentException count > buffer. Length
- index.
ArgumentNullException buffer is null
.ArgumentOutOfRangeException index < 0, or count < 0.
If the end of the character stream in the element is reached before the specified number of characters is read, the return value will be less than count.This method has the following functionality:
[Note: Using this method is the most efficient way to process very large streams of text embedded in an XML document. Rather than allocating large string objects, this method returns text content a buffer at a time.
- It is designed to work on element nodes only; it returns zero for other node types.
- It returns the actual character content including markup. There is no attempt to resolve entities, CDATA, or any other markup encountered.
- It ignores XML markup that is not well-formed. For example, when reading the following XML string
<A>1<A>2</A>
,1<A>2</A>
is returned. (It returns markup from the matching element pair and ignores others.)- It does not do any normalization.
- When it has reached the end of the character stream, the reader is positioned after the end tag.
- Attribute read methods are not available.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string ReadInnerXml();
Reads the contents of the current node, including child nodes and markup.
A String containing the XML content, or System.String.Empty if the current node is neither an element nor attribute, or has no child nodes.
Exception Type Condition XmlException The XML was not well-formed, or an error occurred while parsing the XML.
The current node and corresponding end node are not returned.If the current node is an element, after the call to this method, the reader is positioned after the corresponding end element.
If the current node is an attribute, the position of the reader is not changed.
[Note: If the reader is positioned on a XmlNodeType other than
Element
orAttribute
, calling this method is equivalent to calling the System.Xml.XmlTextReader.Read method.A comparison between calling the System.Xml.XmlTextReader.ReadInnerXml and System.Xml.XmlTextReader.ReadOuterXml methods on a XML fragment is shown below.
Assume the reader is positioned on
<book1
in the following XML fragment.
<books> <book1 id="123" cost="39.95"> Title1 <page1/> </book1> </books>Calling System.Xml.XmlTextReader.ReadInnerXml returns
Title1
Calling System.Xml.XmlTextReader.ReadOuterXml returns<page1/>
<book1 id="123" cost="39.95"> Title1 <page1/> </book1>After either method returns, the reader is positioned on</books>
.Assume the reader is positioned on
id
in the previous XML fragment.Calling System.Xml.XmlTextReader.ReadInnerXml returns
123
Calling System.Xml.XmlTextReader.ReadOuterXml returns
id="123"
After either method returns, the reader is still positioned on
id
.This method overrides System.Xml.XmlReader.ReadInnerXml.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string ReadOuterXml();
Reads the current node and its contents, including child nodes and markup.
A String containing the XML content, or System.String.Empty if the current node is neither an element nor attribute.
Exception Type Condition XmlException The XML was not well-formed, or an error occurred while parsing the XML.
The current node and corresponding end node are returned.If the current node is an element, after the call to this method, the reader is positioned after the corresponding end element.
If the current node is an attribute, the position of the reader is not changed.
[Note: If the reader is positioned on a XmlNodeType other than
Element
orAttribute
, calling this method is equivalent to calling the System.Xml.XmlTextReader.Read method.For a comparison between this method and the System.Xml.XmlTextReader.ReadInnerXml method, see System.Xml.XmlTextReader.ReadInnerXml.
This method overrides System.Xml.XmlReader.ReadOuterXml.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string ReadString();
Reads the contents of an element or a text node as a string.
A String containing the contents of theElement
orText
node, or System.String.Empty if the reader is positioned on any other type of node.
Exception Type Condition InvalidOperationException An invalid operation was attempted. XmlException An error occurred while parsing the XML.
If positioned on anElement
node, this method concatenates allText
,SignificantWhitespace
,Whitespace
, andCDATA
node types, and returns the concatenated data as the element content. If none of these node types exist, System.String.Empty is returned. Concatenation stops when any markup is encountered, which can occur in a mixed content model or when an element end tag is read.If positioned on an element
Text
node, this method performs the same concatenation from theText
node to the element end tag. If the reader is positioned on an attributeText
node, this method has the same functionality as if the reader were position on the element start tag.[Note: This method overrides System.Xml.XmlReader.ReadString.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public void ResetState();
Resets the System.Xml.XmlTextReader.ReadState to System.Xml.ReadState.Initial.
Exception Type Condition InvalidOperationException The current instance was constructed with a XmlParserContext.
The System.Xml.XmlTextReader.Normalization, System.Xml.XmlTextReader.WhitespaceHandling, System.Xml.XmlTextReader.Namespaces, and System.Xml.XmlTextReader.XmlResolver properties are not changed by this method.[Note: This method enables the parsing of multiple XML documents in a single stream. When the end of an XML document is reached, this method resets the state of the current instance in preparation for the next XML document.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override void ResolveEntity();
Resolves the entity reference forEntityReference
nodes.
Exception Type Condition InvalidOperationException Any call to this method.
[Note: XmlTextReader does not support entity resolution.This method overrides System.Xml.XmlReader.ResolveEntity.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override int AttributeCount { get; }
Gets the number of attributes on the current node.
A Int32 containing the number of attributes on the current node, or zero if the current node does not support attributes.
This property is read-only.[Note: This property is relevant to the
DocumentType
,Element
, andXmlDeclaration
node types of the XmlNodeType enumeration. Other node types do not have attributes.This property overrides System.Xml.XmlReader.AttributeCount.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string BaseURI { get; }
Gets the base Uniform Resource Identifier (URI) of the current node.
The base URI of the current node.
This property is read-only.This property is set when the reader is instantiated and defaults to System.String.Empty.
[Note: A networked XML document is comprised of chunks of data aggregated using various W3C standard inclusion mechanisms and therefore contains nodes that come from different places. Document Type Definition (DTD) entities are an example of this, but this is not limited to DTDs. The base URI tells where these nodes come from.
This property overrides System.Xml.XmlReader.BaseURI .
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override int Depth { get; }
Gets the depth of the current node in the XML document.
A Int32 containing the depth of the current node in the XML document.
This property is read-only.[Note: This property overrides System.Xml.XmlReader.Depth.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool EOF { get; }
Gets a value indicating whether the System.Xml.XmlTextReader.ReadState is System.Xml.ReadState.EndOfFile, signifying the reader is positioned at the end of the stream.
A Boolean wheretrue
indicates the reader is positioned at the end of the stream; otherwise,false
.
This property is read-only.[Note: This property overrides System.Xml.XmlReader.EOF.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public Encoding Encoding { get; }
Gets the encoding of the document.
If the System.Xml.XmlTextReader.ReadState is System.Xml.ReadState.Interactive, a Encoding; otherwisenull
.
This property is read-only.If no encoding attribute exists, this property defaults to UTF-8.
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool HasValue { get; }
Gets a value indicating whether the current node can have an associated text value.
A Boolean wheretrue
indicates the node on which the reader is currently positioned can have an associated text value; otherwise,false
.
This property is read-only.The following members of the XmlNodeType enumeration can have an associated value:
Attribute
,CDATA
,Comment
,DocumentType
,ProcessingInstruction
,SignificantWhitespace
,Text
,Whitespace
, andXmlDeclaration
.[Note: This property overrides System.Xml.XmlReader.HasValue.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool IsDefault { get; }
Gets a value indicating whether the current node is an attribute that was generated from the default value defined in the DTD or schema.
This property always returns the Boolean valuefalse
.
This property is read-only.This property applies only to attribute nodes.
[Note: XmlTextReader does not expand default attributes.
This property overrides System.Xml.XmlReader.IsDefault.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override bool IsEmptyElement { get; }
Gets a value indicating whether the current node is an empty element (for example,<MyElement />
).
A Boolean wheretrue
indicates the current node is an element (System.Xml.XmlTextReader.NodeType equals System.Xml.XmlNodeType.Element) that ends with "/>
"; otherwise,false
.
This property is read-only.A System.Xml.XmlNodeType.EndElement node is not generated for empty elements.
[Note: This property determines the difference between the following:
<item bar="123"/>
(IsEmptyElement
istrue
).
<item bar="123">
(IsEmptyElement
isfalse
).This property overrides System.Xml.XmlReader.IsEmptyElement.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string this[int i] { get; }
Retrieves the value of the attribute with the specified index relative to the containing element.
- i
- A Int32 specifying the zero-based index of the attribute relative to the containing element.
A String containing the value of the attribute.
Exception Type Condition ArgumentOutOfRangeException i is less than 0 or greater than or equal to the System.Xml.XmlTextReader.AttributeCount of the containing element.[Note: System.Xml.XmlTextReader.AttributeCount returns zero for all node types except Attribute
,DocumentType
,Element
, andXmlDeclaration
. Therefore, this exception is thrown if the reader is not positioned on one of these node types.]
This property is read-only.This property does not move the reader.
[Note: This property overrides the XmlReader indexer.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string this[string name] { get; }
Retrieves the value of the attribute with the specified qualified name.
- name
- A String specifying the qualified name of the attribute.
A String containing the value of the specified attribute, ornull
if the attribute is not found.
This property is read-only.This property does not move the reader.
[Note: If the reader is positioned on a
DocumentType
node, this method can be used to get the PUBLIC and SYSTEM literals.This property overrides the XmlReader indexer.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string this[string name, string namespaceURI] { get; }
Retrieves the value of the attribute with the specified local name and namespace URI.
- name
- A String specifying the local name of the attribute.
- namespaceURI
- A String specifying the namespace URI of the attribute.
A String containing the value of the specified attribute, ornull
if the attribute is not found.
This property is read-only.This property does not move the reader.
[Note: This property overrides the XmlReader indexer.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public int LineNumber { get; }
Gets the current line number.
A Int32 containing the current line number.
This property is read-only.The constructors initialize this property to one.
[Note: This property is most commonly used for error reporting, but can be called at any time.
The start of a document is indicated when this property is 1 and the System.Xml.XmlTextReader.LinePosition property is 1.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public int LinePosition { get; }
Gets the current position in a line.
A Int32 containing the current line position.
This property is read-only.The constructors initialize this property to one, which indicates the first character of text in a line.
[Note: For example, <root>, contains the character 'r' at System.Xml.XmlTextReader.LinePosition equal to 2 and the character '>' at System.Xml.XmlTextReader.LinePosition equal to 6.
This property is most commonly used for error reporting, but can be called at any time.
The start of a document is indicated when this property is 1 and the System.Xml.XmlTextReader.LineNumber property is 1.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string LocalName { get; }
Gets the local name of the current node.
A String containing the local name of the current node or, for node types that do not have a name (likeText
,Comment
, and so on), System.String.Empty.
This property is read-only.The local name is equivalent to System.Xml.XmlTextReader.Name with System.Xml.XmlTextReader.Prefix and the ':' character removed. For example, System.Xml.XmlTextReader.LocalName is "book" for the element
<bk:book>
.[Note: This property overrides System.Xml.XmlReader.LocalName.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string Name { get; }
Gets the qualified name of the current node.
A String containing the qualified name of the current node or, for node types that do not have a name (likeText
,Comment
, and so on), System.String.Empty.
This property is read-only.The name returned is dependent on the System.Xml.XmlTextReader.NodeType of the node. The following node types return the listed values. All other node types return an empty string.
[Note: The qualified name is equivalent to the System.Xml.XmlTextReader.LocalName prefixed with System.Xml.XmlTextReader.Prefix and the ':' character. For example, System.Xml.XmlTextReader.Name is "bk:book" for the element
Node Type Name Attribute
The name of the attribute. DocumentType
The document type name. Element
The tag name. EntityReference
The name of the entity referenced. ProcessingInstruction
The target of the processing instruction. XmlDeclaration
The literal string "xml". <bk:book>
.This property overrides System.Xml.XmlReader.Name.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override XmlNameTable NameTable { get; }
Gets the name table used by the current instance to store and look up element and attribute names, prefixes, and namespaces.
The XmlNameTable used by the current instance.
This property is read-only.The XmlTextReader class stores element and attribute names, prefixes, and namespaces as individual String objects when a document is read.
A qualified name is stored as a unique String instance and separated into its prefix and local name parts, which are also stored as unique strings instances. For example,
<somePrefix:someElement>
, is stored as three strings, "somePrefix:someElement", "somePrefix", and "someElement".[Note: This property overrides System.Xml.XmlReader.NameTable.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string NamespaceURI { get; }
Gets the namespace URI associated with the node on which the reader is positioned.
A String containing the namespace URI of the current node or, if no namespace URI is associated with the current node, System.String.Empty.
This property is read-only.This property is relevant to
Element
andAttribute
nodes only.[Note: Namespaces conform to the W3C "Namespaces in XML" recommendation, REC-xml-names-19990114.
This property overrides System.Xml.XmlReader.NamespaceURI.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public bool Namespaces { get; set; }
Gets or sets a value indicating whether the reader supports namespaces.
A Boolean wheretrue
indicates the reader supports namespaces; otherwise,false
. The default istrue
.
Exception Type Condition InvalidOperationException When attempting to set the property, the System.Xml.XmlTextReader.ReadState was not System.Xml.ReadState.Initial.
This property determines whether the reader supports the XML Namespaces specification (http://www.w3.org/TR/REC-xml-names). If this property isfalse
, namespaces are ignored and the reader allows names to contain multiple colon characters.If an attempt is made to set this property after a read operation has occurred, a InvalidOperationException is thrown.
System.Xml.XmlTextReader Class, System.Xml Namespace
public override XmlNodeType NodeType { get; }
Gets the XmlNodeType of the current node.
One of the members of the XmlNodeType enumeration representing the type of the current node.
This property is read-only.This property does not return the following XmlNodeType types:
Document
,DocumentFragment
,Entity
,EndEntity
, orNotation
.[Note: This property overrides System.Xml.XmlReader.NodeType.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public bool Normalization { get; set; }
Gets or sets a value indicating whether to normalize white space and attribute values.
A Boolean wheretrue
indicates to normalize; otherwise,false
. The default isfalse
.
Exception Type Condition InvalidOperationException When attempting to set the property, the current instance has been closed.
This property can be changed at any time before the current instance has been closed and takes affect on the next read operation.If System.Xml.XmlTextReader.Normalization is set to
false
, this also disables character range checking for numeric entities. As a result, character entities, such as "&#0
", are allowed.[Note: See "Attribute-Value Normalization" in the W3C XML 1.0 recommendation, REC-xml-19980210.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string Prefix { get; }
Gets the namespace prefix associated with the current node.
A String containing the namespace prefix associated with the current node.
This property is read-only.[Note: A namespace prefix is used as a reference for a namespace URI and is defined in an element declaration. For example,
<someElement xmlns:bk='someURL'>
, defines a prefix name "bk".This property overrides System.Xml.XmlReader.Prefix.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override char QuoteChar { get; }
Gets the quotation mark character used to enclose the value of an attribute.
A Char specifying the quotation mark character (" or ') used to enclose the value of an attribute.
This property is read-only.This property applies only to an
Attribute
node.[Note: This property overrides System.Xml.XmlReader.QuoteChar.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override ReadState ReadState { get; }
Gets the read state of the reader.
One of the members of the ReadState enumeration.
This property is read-only.[Note: This property overrides System.Xml.XmlReader.ReadState.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string Value { get; }
Gets the text value of the current node.
A String containing the text value of the current node.
This property is read-only.The value returned depends on the System.Xml.XmlTextReader.NodeType. The following table lists node types that have a value to return. All other node types return System.String.Empty.
[Note: This property overrides System.Xml.XmlReader.Value.
Node Type Value Attribute
The value of the attribute. CDATA
The content of the CDATA section. Comment
The content of the comment. DocumentType
The internal subset. ProcessingInstruction
The entire content, excluding the target. SignificantWhitespace
The white space in the scope of xml:space = "preserve"
.Text
The content of the text node.
Whitespace
The white space between markup. XmlDeclaration
The content of the declaration. ]
System.Xml.XmlTextReader Class, System.Xml Namespace
public WhitespaceHandling WhitespaceHandling { get; set; }
Gets or sets a value that specifies the type of white space returned by the reader.
One of the members of the WhitespaceHandling enumeration. The default is System.Xml.WhitespaceHandling.All (returns both significant and insignificant white space).
Exception Type Condition ArgumentOutOfRangeException The value to be set is not one of the members of the WhitespaceHandling enumeration. InvalidOperationException When setting the property, the System.Xml.XmlTextReader.ReadState is System.Xml.ReadState.Closed.
This property can be changed at any time before the current instance is closed and takes affect on the next read operation.[Note: Because an instance of the XmlTextReader class does not have DTD information available to it,
SignificantWhitespace
nodes are only returned within thexml:space="preserve"
scope.]
System.Xml.XmlTextReader Class, System.Xml Namespace
public override string XmlLang { get; }
Gets the currentxml:lang
scope.
A String containing the currentxml:lang
scope.
This property is read-only.[Note: This property represents the
xml:lang
scope within which the current node resides. For example, the following is an XML fragment withxml:lang
set to US English:
<root xml:lang="en-us">
When the reader is positioned on the<name>Fred</name>
</root>
name
element, this property returns "en-us".The returned string is also in the System.Xml.XmlTextReader.NameTable for the reader.
This property overrides System.Xml.XmlReader.XmlLang.
]
System.Xml.XmlTextReader Class, System.Xml Namespace
public XmlResolver XmlResolver { set; }
Sets the XmlResolver used for resolving DTD references.
The XmlResolver to use for resolving DTD references.If this property is not set, the current instance uses a new instance of the XmlUrlResolver class with default credentials. If this property is set to
null
, any external DTD or entities encountered by the reader are not resolved.
This property is write-only.The XmlResolver is used to resolve the location of the file loaded into the reader and also to resolve DTD references. For example, if the XML included the DOCTYPE declaration,
<!DOCTYPE book SYSTEM book.dtd>
, the reader resolves this external file and ensures that the DTD is well-formed. XmlTextReader does not use the DTD for validation.This property can be changed at any time and takes affect on the next read operation.
System.Xml.XmlTextReader Class, System.Xml Namespace
public override XmlSpace XmlSpace { get; }
Gets the currentxml:space
scope.
One of the members of the XmlSpace enumeration. If noxml:space
scope exists, this property defaults to System.Xml.XmlSpace.None.
This property is read-only.[Note: The XmlTextReader class has no DTD information available; therefore,
SignificantWhitespace
nodes are only returned when inside the scope ofxml:space = "preserve"
.This property overrides System.Xml.XmlReader.XmlSpace.
]
System.Xml.XmlTextReader Class, System.Xml Namespace