Udostępnij przez


XmlTextReader.ReadChars(Char[], Int32, Int32) Metoda

Definicja

Odczytuje zawartość tekstową elementu do bufora znaków. Ta metoda została zaprojektowana do odczytywania dużych strumieni osadzonego tekstu przez wywołanie go kolejno.

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

Parametry

buffer
Char[]

Tablica znaków, która służy jako bufor, do którego jest zapisywana zawartość tekstu.

index
Int32

Położenie w miejscu, w buffer którym metoda może rozpocząć pisanie zawartości tekstowej.

count
Int32

Liczba znaków do zapisania w pliku buffer.

Zwraca

Liczba odczytanych znaków. Może to być 0 , jeśli czytnik nie jest umieszczony na elemencie lub jeśli nie ma więcej zawartości tekstowej do zwrócenia w bieżącym kontekście.

Wyjątki

count jest większa niż miejsce określone w obiekcie (rozmiar buforu bufferindex).

Wartość elementu buffer to null.

index< 0 lub count< 0.

Przykłady

Poniższy przykład odczytuje kod XML przy użyciu polecenia ReadChars.

#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

W przykładzie użyto items.xml pliku jako danych wejściowych.


<?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>

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu XmlReader.Create metody , aby korzystać z nowych funkcji.

Jest to najbardziej wydajny sposób przetwarzania bardzo dużych strumieni tekstu osadzonego w dokumencie XML. Zamiast przydzielać duże obiekty ciągów, ReadChars zwraca zawartość tekstową bufora naraz. Ta metoda jest przeznaczona do pracy tylko w węzłach elementów. Inne typy węzłów powodują ReadChars zwrócenie wartości 0.

W poniższym kodzie XML, jeśli czytnik jest umieszczony w tagu początkowym, ReadChars zwraca test i umieszcza czytnik po tagu końcowym.

<Item>test</Item>

ReadChars ma następujące funkcje:

  • Ta metoda została zaprojektowana do pracy tylko w węzłach elementów. Inne typy węzłów powodują ReadChars zwrócenie wartości 0.

  • Ta metoda zwraca rzeczywistą zawartość znaku. Nie ma próby rozpoznania jednostek, CDATA ani żadnych innych napotkanych znaczników. ReadChars Metoda zwraca wszystko między tagiem początkowym a tagiem końcowym, w tym znacznikami.

  • ReadChars Ignoruje znaczniki XML, które nie są poprawnie sformułowane. Na przykład podczas odczytywania następującego ciągu <A>1<A>2</A>ReadChars XML zwraca wartość 1<A>2</A>. (Zwraca adiustację z pasującej pary elementów i ignoruje inne).

  • Ta metoda nie wykonuje żadnej normalizacji.

  • Po ReadChars osiągnięciu końca strumienia znaków zwraca wartość 0, a czytnik jest umieszczony po tagu końcowym.

  • Metody odczytu atrybutów nie są dostępne podczas korzystania z programu ReadChars.

Na przykład przy użyciu następującego kodu XML:

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

Czytnik jest umieszczony na elemecie <item> na końcu pętli while.

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.
 }
}

Dotyczy

Zobacz też