Udostępnij za pośrednictwem


Reading Content

The XmlReader class includes members that can be used to read content.

Note

The members described in this topic return content as string values. If you want to read typed content, see Reading Typed Data.

Value Property

The Value property can be used to get the text content of the current node. The value returned depends on the node type of the current node. The following table describes the content returned for each of the possible node types.

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 between any markup in a mixed content model.

Text

The content of the text node.

Whitespace

The white space between markup.

XmlDeclaration

The content of the declaration.

All other node types

An empty string.

ReadString Method

The ReadString method returns the content of an element or text node as a string.

If the XmlReader is positioned on an element, ReadString concatenates all text, significant white space, white space, and CDATA section nodes together and returns the concatenated data as the element content. The reader stops when any markup is encountered. This could occur in a mixed content model, or when an element end tag is read.

If the XmlReader is positioned on a text node, ReadString performs the same concatenation of text, significant white space, white space, and CDATA section nodes. The reader stops on the first node that is not one of the previously named types. If the reader is positioned on an attribute text node, ReadString has the same functionality as if the reader were position on the element start tag. It returns all the concatenated element text nodes.

Note

The ReadString method stops on processing instructions and comments. It does not ignore them.

ReadInnerXml Method

The ReadInnerXml method returns all the content of the current node, including the markup. The current node (start tag) and corresponding end node (end tag) are not returned. For example, if you had the XML string <node>this<child id="123"/></node>, ReadInnerXml would return this<child id="123"/>.

The following table describes how element and attribute nodes are handled.

Node type

Initial

position

XML fragment

Return

value

Position

after

Element

On the item1 start tag.

<item1>text1</item1><item2>text2</item2>

text1

On the item2 start tag.

Attribute

On the attr1 attribute node.

<item attr1="val1" attr2="val2">text</item>

val1

Remains on the attr1 attribute node.

If the reader is positioned on a leaf node, calling ReadInnerXml is equivalent to calling Read.

ReadOuterXml Method

The ReadOuterXml method returns all the XML content, including markup, of the current node and all its children. Its behavior is similar to ReadInnerXml, except it also returns the start and end tags.

Using the values in the table above, if the reader is positioned on the item1 start tag, ReadOuterXml returns <item1>text1</item1>. If the reader is positioned on the attr1 attribute node, ReadOuterXml returns attr1="val1".

See Also

Concepts

Reading Elements

Reading Attributes

Reading XML with the XmlReader