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.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew 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 != nullptr )
            reader->Close();
   }

}
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>

注釈

注意

.NET Framework 2.0 以降では、 メソッドを使用してXmlReader.Create新しい機能を利用してインスタンスを作成XmlReaderすることをお勧めします。

要素に配置されている場合は、 ReadString すべてのテキスト、重要な空白、空白、および CData セクション ノードの種類を連結し、連結されたデータを要素コンテンツとして返します。 コメントや処理命令など、マークアップが検出されると停止します。 これは混合コンテンツ モデル内で、または要素の終了タグが読み込まれると発生します。

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

適用対象

こちらもご覧ください