Skipping Content with XmlReader

Content is skipped in two ways. One way is to call a method that moves directly to content using the MoveToContent method. The other way is to call the Skip method directly, which skips over child nodes from the current node.

Moving Directly to Content

To move to content, use the MoveToContent method. The method checks the current node to see if the node is a content node. A content node is defined to be any Text, CDATA, Element, EndElement, EntityReference, or EndEntity node. If it is not one of the preceding node types, it skips over that node and to the next node or end of file. It continues skipping until it finds a node of that type, or the end of the file, and stops. In other words, it skips over the following node types:

  • XmlDeclaration
  • ProcessingInstruction
  • DocumentType
  • Comment
  • Attribute
  • Whitespace
  • SignificantWhitespace

Using this type of content navigation is more efficient if the application only wants content, instead of calling Read that moves the reader to the next node, and forcing the application to test the node type and determine if there is content to read, and if so read it.

If the application is positioned on an attribute node, then calling MoveToContent moves the current node position to the owning element for that attribute. If the application is already positioned on a content node, then the MoveToContent call returns the value of the NodeType property to the application. These behaviors allow the application to skip over random XML markup. For example, consider the following XML input:

<?xml version="1.0">
<!DOCTYPE price SYSTEM "abc">
<!––the price of the book –->
<price>123.4</price>

The following code finds the price element of "123.4" and converts its text content to a double:

If readr.MoveToContent() = XmlNodeType.Element And readr.Name = "price" Then
   _price = XmlConvert.ToDouble(readr.ReadString())
End If
[C#]
if (readr.MoveToContent() == XmlNodeType.Element && readr.Name =="price")
{
    _price = XmlConvert.ToDouble(readr.ReadString());
}

For another example, the MoveToContent is often used to find the DocumentElement node in an XML file. Assuming the following XML input file called file.xml:

<?xml encoding="utf-8"?>
<!-- Can have comment and DOCTYPE nodes here to skip past here -->
<phone a="2" b="N">
<data>
   <d1>Data</d1>
   <d2>More Data</d2>
   <d3>Some More Data</d3>
</data>
</phone>

the following code positions the reader on the DocumentElement node <phone>:

Dim doc as XmlDocument = New XmlDocument()
Dim treader as New XmlTextReader("file.xml")
treader.MoveToContent()        
[C#]
XmlDocument doc = new XmlDocument();
XmlTextReader treader = new XmlTextReader("file.xml");
treader.MoveToContent();

Skipping over Data Using the Skip Method

The Skip method moves over the current element. If the node type is XmlNodeType.Element, calling Skip moves over all of the content of the element and the element end tag.

For example, if you have the following XML:

<a name="facts" location="123">
<x/> 
abc 
<y/>
</a>
<b>
...
</b>

and you are positioned on the <a> node or any of its attributes, calling Skip positions you on the <b> node. If you are positioned on a leaf node, like x, or the text node abc, then Skip skips to the next node, which means it actually behaves the same as calling Read.

Skip applies the well-formed rules to the content.

See Also

Reading XML with the XmlReader | Current Node Position in XmlReader | Property Settings on XmlReader | Object Comparison Using XmlNameTable with XmlReader | Reading Attributes with XmlReader | Reading Element and Attributes Content | 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