XmlTextReader.ReadChars(Char[], Int32, Int32) 메서드

정의

요소의 텍스트 콘텐츠를 문자 버퍼로 읽습니다. 이 메서드는 뒤이어 호출하면 포함된 텍스트의 큰 스트림을 읽도록 디자인되었습니다.

public:
 int ReadChars(cli::array <char> ^ buffer, int index, int count);
public int ReadChars (char[] buffer, int index, int count);
member this.ReadChars : char[] * int * int -> int
Public Function ReadChars (buffer As Char(), index As Integer, count As Integer) As Integer

매개 변수

buffer
Char[]

텍스트 콘텐츠를 쓸 버퍼 역할을 하는 문자 배열입니다.

index
Int32

메서드가 텍스트 콘텐츠 쓰기를 시작할 수 있는 buffer 내 위치입니다.

count
Int32

buffer로 쓸 문자 수입니다.

반환

Int32

읽은 문자 수입니다. 판독기가 요소에 배치되지 않았거나 현재 컨텍스트에 반환할 텍스트 콘텐츠가 없는 경우 반환 값은 0이 될 수 있습니다.

예외

countbuffer에 지정된 공간(버퍼 크기 - index)보다 큽니다.

buffer 값이 null입니다.

index < 0 또는 count< 0입니다.

예제

다음 예제에서는 을 사용하여 ReadCharsXML로 읽습니다.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;

// Reads an XML document using ReadChars
int main()
{
   XmlTextReader^ reader = nullptr;
   String^ filename = "items.xml";
   try
   {
      
      // Declare variables used by ReadChars
      array<Char>^buffer;
      int iCnt = 0;
      int charbuffersize;
      
      // Load the reader with the data file.  Ignore white space.
      reader = gcnew XmlTextReader( filename );
      reader->WhitespaceHandling = WhitespaceHandling::None;
      
      // Set variables used by ReadChars.
      charbuffersize = 10;
      buffer = gcnew array<Char>(charbuffersize);
      
      // Parse the file.  Read the element content
      // using the ReadChars method.
      reader->MoveToContent();
      while ( (iCnt = reader->ReadChars( buffer, 0, charbuffersize )) > 0 )
      {
         
         // Print out chars read and the buffer contents.
         Console::WriteLine( "  Chars read to buffer:{0}", iCnt );
         Console::WriteLine( "  Buffer: [{0}]", gcnew String( buffer,0,iCnt ) );
         
         // Clear the buffer.
         Array::Clear( buffer, 0, charbuffersize );
      }
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}
using System;
using System.Xml;

// Reads an XML document using ReadChars

public class Sample {

  private const String filename = "items.xml";

  public static void Main() {

    XmlTextReader reader = null;

    try {

      // Declare variables used by ReadChars
      Char []buffer;
      int iCnt = 0;
      int charbuffersize;

      // Load the reader with the data file.  Ignore white space.
      reader = new XmlTextReader(filename);
      reader.WhitespaceHandling = WhitespaceHandling.None;

      // Set variables used by ReadChars.
      charbuffersize = 10;
      buffer = new Char[charbuffersize];

      // Parse the file.  Read the element content
      // using the ReadChars method.
      reader.MoveToContent();
      while ( (iCnt = reader.ReadChars(buffer,0,charbuffersize)) > 0 ) {
        // Print out chars read and the buffer contents.
        Console.WriteLine ("  Chars read to buffer:" + iCnt);
        Console.WriteLine ("  Buffer: [{0}]", new String(buffer,0,iCnt));
        // Clear the buffer.
        Array.Clear(buffer,0,charbuffersize);
      }
    }
    finally {
      if (reader!=null)
        reader.Close();
    }
  }
} // End class
Imports System.Xml

' Reads an XML document using ReadChars
Public Class Sample
    Private Const filename As String = "items.xml"
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            ' Declare variables used by ReadChars
            Dim buffer() As Char
            Dim iCnt As Integer = 0
            Dim charbuffersize As Integer
            
            ' Load the reader with the data file.  Ignore white space.
            reader = New XmlTextReader(filename)
            reader.WhitespaceHandling = WhitespaceHandling.None
            
            ' Set variables used by ReadChars.
            charbuffersize = 10
            buffer = New Char(charbuffersize) {}
            
            ' Parse the file.  Read the element content  
            ' using the ReadChars method.
            reader.MoveToContent()
            iCnt = reader.ReadChars(buffer,0,charbuffersize)
            while (iCnt > 0)
              ' Print out chars read and the buffer contents.
              Console.WriteLine("  Chars read to buffer:" & iCnt)
              Console.WriteLine("  Buffer: [{0}]", New String(buffer, 0, iCnt))
              ' Clear the buffer.
              Array.Clear(buffer, 0, charbuffersize)
              iCnt = reader.ReadChars(buffer,0,charbuffersize)
           end while

        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 
End Class

이 예제에서는 items.xml 파일을 입력으로 사용합니다.


<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>test with a child element <more/> stuff</Item>
  <Item>test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with an char entity: A</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

설명

참고

.NET Framework 2.0부터 새 기능을 활용하기 위해 메서드를 사용하여 XmlReader.Create 인스턴스를 만드는 XmlReader 것이 좋습니다.

XML 문서에 포함된 매우 큰 텍스트 스트림을 처리하는 가장 효율적인 방법입니다. 큰 문자열 개체를 할당하는 대신 텍스트 콘텐츠를 한 번에 버퍼로 반환합니다 ReadChars . 이 메서드는 요소 노드에서만 작동하도록 설계되었습니다. 다른 노드 형식으로 인해 ReadChars 반환 0됩니다.

다음 XML에서 판독기를 시작 태그 ReadChars 에 배치하는 경우 판독기를 반환 test 하고 끝 태그 뒤의 위치를 지정합니다.

<Item>test</Item>  

ReadChars 에는 다음과 같은 기능이 있습니다.

  • 이 메서드는 요소 노드에서만 작동하도록 설계되었습니다. 다른 노드 형식으로 인해 ReadChars 0이 반환됩니다.

  • 이 메서드는 실제 문자 콘텐츠를 반환합니다. 엔터티, CDATA 또는 발생한 다른 태그를 확인하려고 시도하지 않습니다. ReadChars 는 태그를 포함하여 시작 태그와 끝 태그 사이의 모든 항목을 반환합니다.

  • ReadChars 는 올바른 형식이 아닌 XML 태그를 무시합니다. 예를 들어 다음 XML 문자열 <A>1<A>2</A>``ReadChars 을 읽을 때 .를 반환합니다1<A>2</A>. 일치하는 요소 쌍에서 태그를 반환하고 다른 요소를 무시합니다.

  • 이 메서드는 정규화를 수행하지 않습니다.

  • ReadChars 문자 스트림의 끝에 도달하면 값 0을 반환하고 판독기는 끝 태그 다음으로 배치됩니다.

  • 특성을 사용하는 ReadChars동안에는 특성 읽기 메서드를 사용할 수 없습니다.

예를 들어 다음 XML을 사용하세요.

<thing>  
 some text  
</thing>  
<item>  
</item>  

판독기는 while 루프의 <item> 끝에 있는 요소에 배치됩니다.

if (XmlNodeType.Element == reader.NodeType && "thing" == reader.Name)  
{  
 while(0 != reader.ReadChars(buffer, 0, 1)  
 {  
 // Do something.  
 // Attribute values are not available at this point.  
 }  
}  

적용 대상

추가 정보