XmlReader.ReadToDescendant XmlReader.ReadToDescendant XmlReader.ReadToDescendant XmlReader.ReadToDescendant Method

定义

XmlReader 前进到下一个匹配的子代元素。Advances the XmlReader to the next matching descendant element.

重载

ReadToDescendant(String, String) ReadToDescendant(String, String) ReadToDescendant(String, String) ReadToDescendant(String, String)

XmlReader 前进到下一个具有指定的本地名称和命名空间 URI 的子代元素。Advances the XmlReader to the next descendant element with the specified local name and namespace URI.

ReadToDescendant(String) ReadToDescendant(String) ReadToDescendant(String) ReadToDescendant(String)

XmlReader 前进到下一个具有指定限定名的子代元素。Advances the XmlReader to the next descendant element with the specified qualified name.

ReadToDescendant(String, String) ReadToDescendant(String, String) ReadToDescendant(String, String) ReadToDescendant(String, String)

XmlReader 前进到下一个具有指定的本地名称和命名空间 URI 的子代元素。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

参数

localName
String String String String

要移动到的元素的本地名称。The local name of the element you wish to move to.

namespaceURI
String String String String

要移动到的元素的命名空间 URI。The namespace URI of the element you wish to move to.

返回

如果找到匹配的子代元素,则为 true;否则为 falsetrue if a matching descendant element is found; otherwise false. 如果未找到匹配的子元素,XmlReader 将定位在元素的结束标记(NodeTypeXmlNodeType.EndElement)上。If a matching child element is not found, the XmlReader is positioned on the end tag (NodeType is XmlNodeType.EndElement) of the element.

如果调用 XmlReader 时没有将 ReadToDescendant(String, String) 定位在某个元素上,则此方法返回 falseXmlReader 的位置保持不变。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.

异常

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

两个参数值均为 nullBoth parameter values are null.

ReadToDescendant(String) ReadToDescendant(String) ReadToDescendant(String) ReadToDescendant(String)

XmlReader 前进到下一个具有指定限定名的子代元素。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

参数

name
String String String String

要移动到的元素的限定名。The qualified name of the element you wish to move to.

返回

如果找到匹配的子代元素,则为 true;否则为 falsetrue if a matching descendant element is found; otherwise false. 如果未找到匹配的子元素,XmlReader 将定位在元素的结束标记(NodeTypeXmlNodeType.EndElement)上。If a matching child element is not found, the XmlReader is positioned on the end tag (NodeType is XmlNodeType.EndElement) of the element.

如果调用 XmlReader 时没有将 ReadToDescendant(String) 定位在某个元素上,则此方法返回 falseXmlReader 的位置保持不变。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.

异常

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

参数是空字符串。The parameter is an empty string.

示例

下面的示例分析的第二个 book 节点。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

该示例使用该文件,2books.xml作为输入。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>

适用于