XmlReader.MoveToContent Method

Definition

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:
 virtual System::Xml::XmlNodeType MoveToContent();
public virtual System.Xml.XmlNodeType MoveToContent ();
abstract member MoveToContent : unit -> System.Xml.XmlNodeType
override this.MoveToContent : unit -> System.Xml.XmlNodeType
Public Overridable Function MoveToContent () As XmlNodeType

Returns

The NodeType of the current node found by the method or XmlNodeType.None if the reader has reached the end of the input stream.

Exceptions

Incorrect XML encountered in the input stream.

An XmlReader method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

Examples

This is useful when you want to write code that can skip over random XML markup without breaking. For example, suppose you have the following code:

if ( reader->MoveToContent() == XmlNodeType::Element &&
   reader->Name->Equals( "price" ) )
{
   _price = reader->ReadString();
}
if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "price") 
 {
    _price = reader.ReadString();
 }
If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "price" Then
    _price = reader.ReadString()
End If

This code can handle the following inputs without breaking:

<price>123.4</price>  

and

<?xml version="1.0"><!DOCTYPE price SYSTEM  
  "abc"><price>123.4</price>  

and

<?xml version="1.0"><!DOCTYPE price SYSTEM "abc"  
  [<!ENTITY p  
  "123.4">]><price>&p;</price>  

and

<!-- some test comment --><?processing  
  instruction?><price>123.4</price>  

Remarks

If the current node is an attribute node, this method moves the reader back to the element that owns the attribute.

For the asynchronous version of this method, see MoveToContentAsync.

Applies to