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)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs

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

C#
public virtual bool ReadToDescendant(string localName, string namespaceURI);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ReadToDescendant(String)

Source:
XmlReader.cs
Source:
XmlReader.cs
Source:
XmlReader.cs

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

C#
public virtual bool ReadToDescendant(string name);

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.

C#
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());
}

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

XML
<!--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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0