XmlReader.ReadToDescendant Method

Definition

Advances the XmlReader to the next matching descendant element.

Overloads

ReadToDescendant(String, String)

Advances the XmlReader to the next descendant element with the specified local name and namespace URI.

ReadToDescendant(String)

Advances the XmlReader to the next descendant element with the specified qualified name.

ReadToDescendant(String, String)

Advances the XmlReader to the next descendant element with the specified local name and namespace URI.

public:
 virtual bool ReadToDescendant(System::String ^ localName, System::String ^ namespaceURI);
public virtual bool ReadToDescendant (string localName, string namespaceURI);
abstract member ReadToDescendant : string * string -> bool
override this.ReadToDescendant : string * string -> bool
Public Overridable Function ReadToDescendant (localName As String, namespaceURI As String) As Boolean

Parameters

localName
String

The local name of the element you wish to move to.

namespaceURI
String

The namespace URI of the element you wish to move to.

Returns

true if a matching descendant element is found; otherwise false. If a matching descendant element is not found, the XmlReader is positioned on the end tag (NodeType is XmlNodeType.EndElement) of the element.

If the XmlReader is not positioned on an element when ReadToDescendant(String, String) was called, this method returns false and the position of the XmlReader is not changed.

Exceptions

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."

Both parameter values are null.

Applies to

ReadToDescendant(String)

Advances the XmlReader to the next descendant element with the specified qualified name.

public:
 virtual bool ReadToDescendant(System::String ^ name);
public virtual bool ReadToDescendant (string name);
abstract member ReadToDescendant : string -> bool
override this.ReadToDescendant : string -> bool
Public Overridable Function ReadToDescendant (name As String) As Boolean

Parameters

name
String

The qualified name of the element you wish to move to.

Returns

true if a matching descendant element is found; otherwise false. If a matching descendant element is not found, the XmlReader is positioned on the end tag (NodeType is XmlNodeType.EndElement) of the element.

If the XmlReader is not positioned on an element when ReadToDescendant(String) was called, this method returns false and the position of the XmlReader is not changed.

Exceptions

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."

The parameter is an empty string.

Examples

The following example parses the second book node.

using (XmlReader reader = XmlReader.Create("2books.xml")) {

  // Move the reader to the second book node.
  reader.MoveToContent();
  reader.ReadToDescendant("book");
  reader.Skip(); //Skip the first book.

  // Parse the file starting with the second book node.
  do {
     switch (reader.NodeType) {
        case XmlNodeType.Element:
           Console.Write("<{0}", reader.Name);
           while (reader.MoveToNextAttribute()) {
               Console.Write(" {0}='{1}'", reader.Name, reader.Value);
           }
           Console.Write(">");
           break;
        case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
        case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
     }
  }  while (reader.Read());
}
Using reader As XmlReader = XmlReader.Create("2books.xml")

  ' Move the reader to the second book node.
  reader.MoveToContent()
  reader.ReadToDescendant("book")
  reader.Skip() 'Skip the first book.
  ' Parse the file starting with the second book node.
  Do
    Select Case reader.NodeType
      Case XmlNodeType.Element
        Console.Write("<{0}", reader.Name)
        While reader.MoveToNextAttribute()
            Console.Write(" {0}='{1}'", reader.Name, reader.Value)
        End While
        Console.Write(">")
      Case XmlNodeType.Text
        Console.Write(reader.Value)
      Case XmlNodeType.EndElement
        Console.Write("</{0}>", reader.Name)
    End Select
  Loop While reader.Read()

End Using

The example uses the file, 2books.xml, as input.

<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

Applies to