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

Definice

Načte textový obsah elementu do vyrovnávací paměti znaků. Tato metoda je určena ke čtení velkých proudů vloženého textu voláním postupně.

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[]

Pole znaků, které slouží jako vyrovnávací paměť, do které je textový obsah zapsán.

index
Int32

Pozice v rámci buffer , kde může metoda začít psát textový obsah.

count
Int32

Počet znaků, které se mají zapsat do buffer.

Návraty

Počet přečtených znaků. K tomu může dojít 0 v případě, že čtenář není umístěn na elementu nebo pokud není k dispozici žádný další textový obsah, který by se v aktuálním kontextu mohl vrátit.

Výjimky

count je větší než místo zadané v parametru buffer (velikost vyrovnávací paměti – index).

Hodnota buffer je null.

index< 0 nebo count< 0.

Příklady

Následující příklad čte kód XML pomocí ReadCharspříkazu .

#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

V příkladu se items.xml jako vstup použije soubor .


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

Poznámky

Poznámka

Počínaje rozhraním .NET Framework 2.0 doporučujeme vytvářet XmlReader instance pomocí XmlReader.Create metody , abyste mohli využívat nové funkce.

Jedná se o nejúčinnější způsob zpracování velmi velkých datových proudů textu vložených do dokumentu XML. Místo přidělování velkých řetězcových ReadChars objektů vrátí textový obsah jako vyrovnávací paměť. Tato metoda je navržena tak, aby fungovala pouze na uzlech prvků. Jiné typy uzlů způsobují ReadChars , že se vrátí 0.

V následujícím kódu XML, pokud je čtečka umístěna na počáteční značce, ReadChars vrátí test a umístí čtečku za koncovou značku.

<Item>test</Item>

ReadChars má následující funkce:

  • Tato metoda je navržena tak, aby fungovala pouze na uzlech prvků. Jiné typy uzlů způsobují, že ReadChars se vrátí 0.

  • Tato metoda vrátí skutečný obsah znaků. Nedochází k žádnému pokusu o překlad entit, CDATA nebo jiných značek. ReadChars vrátí všechno mezi počáteční a koncovou značkou, včetně značek.

  • ReadChars ignoruje kód XML, který není ve správném formátu. Například při čtení následujícího řetězce <A>1<A>2</A>XML , ReadChars vrátí 1<A>2</A>. (Vrátí kód z odpovídající dvojice prvků a ostatní ignoruje.)

  • Tato metoda neprovádí žádnou normalizaci.

  • Když ReadChars dosáhne konce znakového proudu, vrátí hodnotu 0 a čtenář se umístí za koncovou značku.

  • Metody čtení atributů nejsou při použití ReadCharsk dispozici.

Například pomocí následujícího kódu XML:

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

Čtečka je umístěna na <item> prvku na konci smyčky 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.
 }
}

Platí pro

Viz také