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

Definice

Přečte textový obsah prvku do vyrovnávací paměti znaků. Tato metoda je navržená tak, aby četla velké datové proudy vloženého textu následným voláním.

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é se textový obsah zapisuje.

index
Int32

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

count
Int32

Počet znaků k zápisu do buffer.

Návraty

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

Výjimky

count je větší než mezera zadaná v buffer (velikost vyrovnávací paměti - index).

Hodnota buffer je null.

index < 0 nebo count< 0.

Příklady

Následující příklad čte v jazyce XML pomocí ReadChars.

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

Příklad používá items.xml soubor jako vstup.


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

Note

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 vyrovnávací paměť najednou. Tato metoda je navržená tak, aby fungovala pouze na uzlech elementů. Jiné typy uzlů způsobují ReadChars vrácení 0.

Pokud je čtečka umístěna na počáteční značce, ReadChars vrátí test čtečku a umístí ji za koncovou značku.

<Item>test</Item>

ReadChars má následující funkce:

  • Tato metoda je navržená tak, aby fungovala pouze na uzlech prvků. Jiné typy uzlů způsobují ReadChars vrácení hodnoty 0.

  • Tato metoda vrátí skutečný obsah znaku. Neexistuje žádný pokus o překlad entit, CDATA ani žádné jiné značky, ke kterým došlo. ReadChars vrátí vše mezi počáteční a koncovou značkou, včetně značky.

  • 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 vrátí ReadChars1<A>2</A>. (Vrátí kód z páru odpovídajících prvků a ignoruje ostatní.)

  • Tato metoda neprovádí normalizaci.

  • Po ReadChars dosažení konce znakového streamu 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é