This topic has not yet been rated - Rate this topic

XmlReader Helper Methods

The following sections describe some additional methods on the XmlReader class that are designed to make it easier to parse data. You can also review the Reading Elements, Reading Attributes, Reading Content, and Reading Typed Data topics to learn about methods and properties that are specific to these tasks.

This method is used to skip the subnodes of the current node. If the reader is currently positioned on a leaf node, calling Skip is the same as calling Read.

For example, in the following XML string,

<a name="bob" age="123"> <x/>abc<y/> </a> <b> ... </b>

If the reader is positioned on the <a> node or any of its attributes, calling Skip positions the reader to the <b> node.

If the reader is positioned on a leaf node already (such as the <x> node or the abc text node), calling Skip is the same as calling Read.

The MoveToContent method is used to skip non-content nodes. Content nodes include non-white space text, CDATA, EntityReference , or EndEntity nodes. If the reader is positioned on a non-content node, MoveToContent moves the reader ahead to the next content node, or to the end of the file.

It skips over nodes of the following type: ProcessingInstruction, DocumentType, Comment, Whitespace, or SignificantWhitespace.

MoveToContent returns the XmlNodeType of the content node that the reader has moved to, or None if the reader has reached the end of the input stream.

For example, given the following XML string:

<?xml version='1.0'?>
<!-- XML fragment -->
<book>
   <price>12.95</price>
</book>

If the reader is positioned at the beginning of the XML string calling MoveToContent moves the reader to the book element. You can also use the MoveToContent method to do conditional processing. The following code moves the reader to a content node, and then checks to see if the node is an element named price.

if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "price")) {
  // Add code to process the price element.
}

The ReadSubtree method can be used to read an element and all its descendants. This method returns a new XmlReader instance set to Initial. A call to the Read method positions the new reader on the node that was current before the call to ReadSubtree method.

Note Note

You should not perform any operations on the original reader until the new reader has been closed. This action is not supported and can result in unpredictable behavior.

Once the entire subtree has been read, calls to the Read method return false. Once the new reader has been closed, the original reader is positioned at the next node past the node that was current prior to the ReadSubtree call.

The ReadSubtree method is useful when you want to pass a reader to another component and you want to make sure that the component is only able to read the current node and its subtree and not more (that is, the component is not able to move the reader past the current subtree.) The XmlReader returned by ReadSubtree limits the nodes that another component can read.

The ReadSubtree method is also useful when a component does not consume the whole subtree. In this case, when the subtree XmlReader has been closed, the original XmlReader is positioned on the next node past the node that was current prior to the ReadSubtree call.

For example, if you had an XML string that had the following structure:

<root>
  <elem>
   (additional nodes here)
  </elem>
  <!-- some comment -->

If the reader is positioned on the elem element, a call to ReadSubtree returns a new reader in the initial state that can be used to parse the entire elem node, and all its descendants. Once this new reader is closed, the original reader is positioned on the comment node and can be used to parse the rest of the XML data.

Did you find this helpful?
(1500 characters remaining)