XmlReader.Skip 方法

定义

跳过当前节点的子级。Skips the children of the current node.

public:
 virtual void Skip();
public virtual void Skip ();
abstract member Skip : unit -> unit
override this.Skip : unit -> unit
Public Overridable Sub Skip ()

例外

在上一次异步操作完成之前调用了 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."

示例

下面的示例分析从第二个 book 节点开始的 XML 文件。The following example parses an XML file starting on 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>

注解

在以下 XML 输入中,如果读取器定位在 <a> 节点或它的任何属性上,则调用会将 Skip 读取器定位到 <b> 节点。In the following XML input if the reader is positioned on the <a> node or any of its attributes, calling Skip positions the reader to the <b> node.

如果读取器位于已 (的叶节点上(例如 <x> 节点或文本节点 abc) ),则调用与 Skip 调用相同 ReadIf the reader is positioned on a leaf node already (such as the <x> node or the text node abc), calling Skip is the same as calling Read.

<a name="bob" age="123">  
 <x/>abc<y/>  
</a>  
<b>  
...  
</b>  

此方法检查格式正确的 XML。This method checks for well-formed XML.

如果读取器是 XmlValidatingReader ,则此方法还会验证已跳过的内容。If the reader is an XmlValidatingReader, this method also validates the skipped content.

XmlReader实现确定 Skip 方法是否将展开外部实体。The XmlReader implementation determines whether or not the Skip method will expand external entities. 下表描述了是否对各种类型的对象展开外部实体 XmlReaderThe following table describes whether the external entities are expanded for the various types of XmlReader objects.

XmlReader 的类型Type of XmlReader 展开外部实体Expands external entities
XmlTextReader 不是。No.
XmlReaderCreate 读取文本数据的方法创建的实例。XmlReader instance created by the Create method that is reading text data. 不是。No.
XmlReaderCreate 读取二进制数据的方法创建的实例。XmlReader instance created by the Create method that is reading binary data. 不适用。Not applicable.
XmlReader通过方法创建的架构验证实例 CreateA schema validating XmlReader instance created by the Create method. 可以。Yes.
XmlValidatingReader 可以。Yes.
XmlReader 对象返回的实例 XPathNavigatorXmlReader instance returned by a XPathNavigator object. 不适用。Not applicable.
XmlNodeReader 不是。No.
XmlReader 在另一个 XmlReader 实例中换行的实例。XmlReader instance wrapped around another XmlReader instance. 依赖于基础的实现 XmlReaderDepends on the implementation of the underlying XmlReader. (Skip 基础上的方法 XmlReader) 称为。(The Skip method on the underlying XmlReader is called).

有关此方法的异步版本,请参阅 SkipAsyncFor the asynchronous version of this method, see SkipAsync.

适用于