XmlNodeReader.ReadString 메서드

정의

요소 또는 텍스트 노드의 내용을 문자열로 읽습니다.

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

반환

String

element나 text와 유사한 노드(CDATA, Text 노드 등)의 내용입니다. 판독기가 요소 또는 텍스트 노드 이외의 위치에 있거나 현재 컨텍스트에 반환할 텍스트 콘텐츠가 없는 경우 이것은 빈 문자열입니다.

Note: 텍스트 노드는 요소 또는 특성 텍스트 노드입니다.

예제

다음 예제에서는 각 요소의 텍스트 내용을 표시합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlNodeReader^ reader = nullptr;
   try
   {
      
      //Create and load the XML document.
      XmlDocument^ doc = gcnew XmlDocument;
      doc->LoadXml( "<book>"
      "<title>Pride And Prejudice</title>"
      "<price>19.95</price>"
      "<misc/>"
      "</book>" );
      
      //Load the XmlNodeReader 
      reader = gcnew 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 != nullptr )
            reader->Close();
   }

}
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

설명

참고

.NET Framework 2.0에서는 클래스와 Create 메서드를 사용하여 인스턴스를 XmlReaderSettings 만드는 XmlReader 것이 좋습니다. 이렇게 하면 .NET Framework 도입된 모든 새로운 기능을 최대한 활용할 수 있습니다. 자세한 내용은 참조 페이지의 설명 섹션을 XmlReader 참조하세요.

요소에 배치된 경우 모든 텍스트, ReadString 중요한 공백, 공백 및 CData 섹션 노드 형식을 함께 연결하고 연결된 데이터를 요소 콘텐츠로 반환합니다. 태그가 발견되면 중지됩니다. 이러한 동작은 혼합 내용 모델에서 발생하거나 요소 끝 태그를 읽을 때 발생할 수 있습니다.

텍스트와 유사한 노드에 배치된 경우 텍스트 노드 ReadString 에서 요소 끝 태그로 동일한 연결을 수행합니다. 판독기가 특성 텍스트 노드에 있을 경우 ReadString에는 판독기가 요소 시작 태그에 있을 때와 같은 기능이 있습니다. 연결된 모든 요소 텍스트 노드를 반환합니다.

적용 대상