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에는 판독기가 요소 시작 태그에 있을 때와 같은 기능이 있습니다. 연결된 모든 요소 텍스트 노드를 반환합니다.

적용 대상

추가 정보