XmlValidatingReader.ReadString 메서드

정의

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

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

반환

요소 또는 텍스트 노드의 내용입니다. 판독기가 요소 또는 텍스트 노드 이외의 위치에 있거나 현재 컨텍스트에 반환할 텍스트 콘텐츠가 없는 경우 이것은 빈 문자열입니다.

예제

다음 예제에서는 각 요소의 텍스트 콘텐츠를 표시합니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ txtreader = nullptr;
   XmlValidatingReader^ reader = nullptr;
   try
   {
      
      //Implement the readers.
      txtreader = gcnew XmlTextReader( "elems.xml" );
      reader = gcnew XmlValidatingReader( txtreader );
      
      //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 txtreader = null;
    XmlValidatingReader reader = null;

    try
    {
       //Implement the readers.
       txtreader = new XmlTextReader("elems.xml");
       reader = new XmlValidatingReader(txtreader);

       //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
Imports System.IO
Imports System.Xml

Public Class Sample
   
   Public Shared Sub Main()
      Dim txtreader As XmlTextReader = Nothing
      Dim reader As XmlValidatingReader = Nothing
      
      Try
         'Implement the readers.
         txtreader = New XmlTextReader("elems.xml")
         reader = New XmlValidatingReader(txtreader)
         
         '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>

설명

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

참고

클래스는 XmlValidatingReader .NET Framework 2.0에서 사용되지 않습니다. 클래스 및 메서드를 XmlReader 사용하여 유효성 검사 인스턴스를 XmlReaderSettingsCreate 만들 수 있습니다. 자세한 내용은 XmlReader 참조 페이지의 설명 섹션을 참조하세요.

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

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

속성은 EntityHandling 다음과 같은 작동 방식을 ReadString 결정합니다.

Description
ExpandEntities 확장된 문자 및 일반 엔터티를 반환합니다. 기본값입니다.
ExpandCharEntities 일반 엔터티 참조를 포함하지만 포함하지 않는 텍스트 콘텐츠를 반환합니다. 즉, 일반 엔터티로 인해 ReadString이 중지됩니다. 를 호출 Read 하여 엔터티 참조를 단계별로 실행해야 합니다.

적용 대상

추가 정보