語言

XmlNodeReader.Skip 方法

定義

跳過目前節點的子節點。

public:
 override void Skip();
public override void Skip();
override this.Skip : unit -> unit
Public Overrides Sub Skip ()

範例

以下範例讀取 XML 文件中的價格元素節點。

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlNodeReader reader = null;

    try
    {
       //Create and load the XML document.
       XmlDocument doc = new XmlDocument();
       doc.LoadXml("<!-- sample XML -->" +
                   "<book>" +
                   "<title>Pride And Prejudice</title>" +
                   "<price>19.95</price>" +
                   "</book>");

       //Load the XmlNodeReader
       reader = new XmlNodeReader(doc);

       reader.MoveToContent(); //Move to the book node.
       reader.Read();  //Read the book start tag.
       reader.Skip();   //Skip the title element.

       Console.WriteLine(reader.ReadOuterXml());  //Read the price element.
     }

     finally
     {
        if (reader != null)
          reader.Close();
      }
  }
} // End class
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlNodeReader = Nothing
        
        Try
            'Create and load the XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<!-- sample XML -->" & _
                       "<book>" & _
                       "<title>Pride And Prejudice</title>" & _
                       "<price>19.95</price>" & _
                       "</book>")
            
            'Load the XmlNodeReader 
            reader = New XmlNodeReader(doc)
            
            reader.MoveToContent() 'Move to the book node.
            reader.Read() 'Read the book start tag.
            reader.Skip() 'Skip the title element.
            Console.WriteLine(reader.ReadOuterXml()) 'Read the price element.
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

備註

Note

為了利用新功能,建議的做法是使用XmlReader該類別和該XmlReaderSettings方法來建立Create實例。 欲了解更多資訊,請參閱參考頁面的 XmlReader 備註區。

舉例來說,假設你有以下 XML 輸入:

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

如果讀取器位於「<a>」節點或其任何屬性上,呼叫 Skip 會將讀取器定位到「<b>」節點。

如果讀取器已經位於葉節點(例如元素「x」或文字節點「abc」),呼叫 Skip 與呼叫 Read是相同的。

此方法檢查 XML 是否為良好格式。

適用於