次の方法で共有


XmlNodeReader.ReadString メソッド

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

Overrides Public Function ReadString() As String
[C#]
public override string ReadString();
[C++]
public: String* ReadString();
[JScript]
public override function ReadString() : String;

戻り値

要素ノードまたはテキストのようなノード (CDATA、Text ノード、など) の内容。要素ノードまたはテキスト ノード以外にリーダーが配置されている場合、または返す対象となるテキスト コンテンツが現在のコンテキスト内にこれ以上ない場合は、これが空の文字列になる場合があります。

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

解説

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

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

使用例

[Visual Basic, C#, C++] 各要素のテキストの内容を表示する例を次に示します。

 
Option Strict
Option Explicit

Imports System
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 'Main 
End Class 'Sample

[C#] 
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

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlNodeReader* reader = 0;

   try
   {
      //Create and load the XML document.
      XmlDocument* doc = new XmlDocument();
      doc->LoadXml(S"<book>" 
                   S"<title>Pride And Prejudice</title>" 
                   S"<price>19.95</price>" 
                   S"<misc/>" 
                   S"</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(S"<{0}/>",reader->Name);
            else{
               Console::Write(S"<{0}> ",reader->Name);
               reader->Read(); //Read the start tag.
               if (reader->IsStartElement())  //Handle nested elements.
                  Console::Write(S"\r\n<{0}>",reader->Name);
               Console::WriteLine(reader->ReadString());  //Read the text content of the element.
            }
         }
      } 

   } 

   __finally 
   {
      if (reader != 0)
         reader->Close();
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlNodeReader クラス | XmlNodeReader メンバ | System.Xml 名前空間