XmlTextReader.ReadString 方法

定義

將元素或文字節點的內容讀成字串。

public:
 override System::String ^ ReadString();
public override string ReadString();
override this.ReadString : unit -> string
Public Overrides Function ReadString () As String

傳回

元素或文字節點的內容。 如果讀取器位於非元素或文字節點上,或當前上下文中沒有更多文字內容可回傳,則該字串可能是空字串。

Note: 文字節點可以是元素或屬性文字節點。

例外狀況

解析 XML 時發生錯誤。

嘗試了一次無效的手術。

範例

以下範例顯示每個元素的文字內容。

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

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

    try
    {
       //Load the reader with the XML file.
       reader = new XmlTextReader("elems.xml");

       //Parse the XML and display the text content of each of the elements.
       while (reader.Read()){
         if (reader.IsStartElement()){
           if (reader.IsEmptyElement)
                    {
                        Console.WriteLine("<{0}/>", reader.Name);
                    }
                    else
                    {
               Console.Write("<{0}> ", reader.Name);
               reader.Read(); //Read the start tag.
               if (reader.IsStartElement())  //Handle nested elements.
                 Console.Write("\r\n<{0}>", reader.Name);
               Console.WriteLine(reader.ReadString());  //Read the text content of the element.
           }
         }
       }
     }

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

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("elems.xml")
            
            'Parse the XML and display the text content of each of the elements.
            While reader.Read()
                If reader.IsStartElement() Then
                    If reader.IsEmptyElement Then
                        Console.WriteLine("<{0}/>", reader.Name)
                    Else
                        Console.Write("<{0}>" + " ", reader.Name)
                        reader.Read() 'Read the start tag.
                        If (reader.IsStartElement())  'Handle nested elements.
                          Console.WriteLine()
                          Console.Write("<{0}>", reader.Name)
                        End If
                        Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
                    End If
                End If
            End While
        
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

範例中使用檔案 elems.xml,作為輸入。


<book>
  <title>Pride And Prejudice</title>
  <price>19.95</price>
  <misc/>
</book>

備註

Note

建議您使用 XmlReader 方法來建立XmlReader.Create實例,以利用新功能。

若置於元素上,則 ReadString 會將所有文字、顯著空白、空白 CData 及區段節點類型串接在一起,並將串接的資料作為元素內容回傳。 當遇到任何標記,包括註解和處理指令時,它就會停止。 這可能發生在混合內容模型中,或當讀取元素結束標籤時。

若置於文字節點上, ReadString 則執行從文字節點到元素結束標籤的相同串接。 如果讀取器位於屬性文字節點上, ReadString 則具有與讀取器位於元素起始標籤相同的功能。 它會回傳所有串接的元素文字節點。

適用於

另請參閱