XmlNodeReader.ReadString メソッド

定義

要素またはテキスト ノードの内容を文字列として読み取ります。

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

返品

要素またはテキストに似たノードの内容 (CDATA、テキスト ノードなどを含めることができます)。 リーダーが要素またはテキスト ノード以外の場所に配置されている場合、または現在のコンテキストで返すテキスト コンテンツがこれ以上ない場合は、空の文字列を指定できます。

Note: テキスト ノードには、要素または属性テキスト ノードのいずれかを指定できます。

次の例では、各要素のテキスト コンテンツを表示します。

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("<book>" +
                   "<title>Pride And Prejudice</title>" +
                   "<price>19.95</price>" +
                   "<misc/>" +
                   "</book>");

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

       //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 XmlNodeReader = Nothing
        
        Try
            'Create and load the XML document.
            Dim doc As New XmlDocument()
            doc.LoadXml("<book>" & _
                        "<title>Pride And Prejudice</title>" & _
                        "<price>19.95</price>" & _
                        "<misc/>" & _
                        "</book>")
            
            'Load the XmlNodeReader 
            reader = New XmlNodeReader(doc)
            
            '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

注釈

Note

新しい機能を利用するには、XmlReader クラスと XmlReaderSettings メソッドを使用してCreate インスタンスを作成することをお勧めします。 詳細については、「 XmlReader リファレンス」ページの「解説」セクションを参照してください。

要素に配置されている場合、 ReadString は、すべてのテキスト、有意な空白、空白、および CData セクションノードタイプを連結し、連結されたデータを要素コンテンツとして返します。 マークアップが検出されると停止します。 これは、混合コンテンツ モデル、または要素の終了タグが読み取られた場合に発生する可能性があります。

テキストのようなノードに配置されている場合、 ReadString はテキスト ノードから要素の終了タグへの同じ連結を実行します。 リーダーが属性テキスト ノードに配置されている場合、 ReadString はリーダーが要素の開始タグの位置にある場合と同じ機能を持ちます。 すべての連結された要素テキスト ノードが返されます。

適用対象