Assembly: System.Xml (in system.xml.dll)
Public Function GetRemainder As TextReader
Dim instance As XmlTextReader Dim returnValue As TextReader returnValue = instance.GetRemainder
public TextReader GetRemainder ()
public:
TextReader^ GetRemainder ()
public TextReader GetRemainder ()
public function GetRemainder () : TextReader
Valore restituito
Oggetto TextReader contenente la parte restante del codice XML memorizzato nella cache. Nota |
|---|
| Nella versione Microsoft .NET Framework versione 2.0 è consigliabile creare istanze di XmlReader utilizzando il metodo System.Xml.XmlReader.Create. In questo modo è possibile sfruttare completamente le nuove funzionalità introdotte in questa versione. Per ulteriori informazioni, vedere Creazione di lettori XML. |
Poiché XmlTextReader esegue un'operazione Read memorizzata nella cache, deve poter restituire la parte restante del buffer inutilizzato in modo da impedire perdite di dati. In tal modo protocolli, quali il MIME in più parti, potranno creare pacchetti XML nello stesso flusso di altri pacchetti.
Dopo la chiamata a questo metodo, il valore della proprietà EOF è impostato su true.
Nell'esempio che segue viene letta la prima parte di un documento XML, quindi viene utilizzato GetRemainder per completare la lettura del documento con un secondo visualizzatore.
Imports System Imports System.Xml Public Class Sample Private Shared filename As String = "tworeads.xml" Public Shared Sub Main() Dim reader As New XmlTextReader(filename) reader.WhitespaceHandling = WhitespaceHandling.None ' Read the first part of the XML document While reader.Read() ' Display the elements and stop reading on the book endelement tag ' then go to ReadPart2 to start another reader to read the rest of the file. Select Case reader.NodeType Case XmlNodeType.Element Console.WriteLine("Name: {0}", reader.Name) Case XmlNodeType.Text Console.WriteLine(" Element Text: {0}", reader.Value) Case XmlNodeType.EndElement ' Stop reading when the reader gets to the end element of the book node. If "book" = reader.LocalName Then Console.WriteLine("End reading first book...") Console.WriteLine() GoTo ReadPart2 End If End Select End While ' Read the rest of the XML document ReadPart2: Console.WriteLine("Begin reading second book...") ' Create a new reader to read the rest of the document. Dim reader2 As New XmlTextReader(reader.GetRemainder()) While reader2.Read() Select Case reader2.NodeType Case XmlNodeType.Element Console.WriteLine("Name: {0}", reader2.Name) Case XmlNodeType.Text Console.WriteLine(" Element Text: {0}", reader2.Value) Case XmlNodeType.EndElement 'Stop reading when the reader gets to the end element of the book node. If "book" = reader2.LocalName Then Console.WriteLine("End reading second book...") GoTo Done End If End Select End While Done: Console.WriteLine("Done.") reader.Close() reader2.Close() End Sub 'Main End Class 'Sample
using System; using System.Xml; public class Sample { private static string filename = "tworeads.xml"; public static void Main() { XmlTextReader reader = new XmlTextReader(filename); reader.WhitespaceHandling=WhitespaceHandling.None; // Read the first part of the XML document while(reader.Read()) { // Display the elements and stop reading on the book endelement tag // then go to ReadPart2 to start another reader to read the rest of the file. switch(reader.NodeType) { case XmlNodeType.Element: Console.WriteLine("Name: {0}", reader.Name); break; case XmlNodeType.Text: Console.WriteLine(" Element Text: {0}", reader.Value); break; case XmlNodeType.EndElement: // Stop reading when the reader gets to the end element of the book node. if ("book"==reader.LocalName) { Console.WriteLine("End reading first book..."); Console.WriteLine(); goto ReadPart2; } break; } } // Read the rest of the XML document ReadPart2: Console.WriteLine("Begin reading second book..."); // Create a new reader to read the rest of the document. XmlTextReader reader2 = new XmlTextReader(reader.GetRemainder()); while(reader2.Read()) { switch (reader2.NodeType) { case XmlNodeType.Element: Console.WriteLine("Name: {0}", reader2.Name); break; case XmlNodeType.Text: Console.WriteLine(" Element Text: {0}", reader2.Value); break; case XmlNodeType.EndElement: // Stop reading when the reader gets to the end element of the book node. if ("book"==reader2.LocalName) { Console.WriteLine("End reading second book..."); goto Done; } break; } } Done: Console.WriteLine("Done."); reader.Close(); reader2.Close(); } }//End class
#using <System.Xml.dll> using namespace System; using namespace System::Xml; int main() { String^ filename = "tworeads.xml"; XmlTextReader^ reader = gcnew XmlTextReader( filename ); reader->WhitespaceHandling = WhitespaceHandling::None; // Read the first part of the XML document while ( reader->Read() ) { // Display the elements and stop reading on the book endelement tag // then go to ReadPart2 to start another reader to read the rest of the file. switch ( reader->NodeType ) { case XmlNodeType::Element: Console::WriteLine( "Name: {0}", reader->Name ); break; case XmlNodeType::Text: Console::WriteLine( " Element Text: {0}", reader->Value ); break; case XmlNodeType::EndElement: // Stop reading when the reader gets to the end element of the book node. if ( "book" == reader->LocalName ) { Console::WriteLine( "End reading first book..." ); Console::WriteLine(); goto ReadPart2; } break; } } // Read the rest of the XML document ReadPart2: Console::WriteLine( "Begin reading second book..." ); // Create a new reader to read the rest of the document. XmlTextReader^ reader2 = gcnew XmlTextReader( reader->GetRemainder() ); while ( reader2->Read() ) { switch ( reader2->NodeType ) { case XmlNodeType::Element: Console::WriteLine( "Name: {0}", reader2->Name ); break; case XmlNodeType::Text: Console::WriteLine( " Element Text: {0}", reader2->Value ); break; case XmlNodeType::EndElement: // Stop reading when the reader gets to the end element of the book node. if ( "book" == reader2->LocalName ) { Console::WriteLine( "End reading second book..." ); goto Done; } break; } } Done: Console::WriteLine( "Done." ); reader->Close(); reader2->Close(); }
import System.*;
import System.Xml.*;
public class Sample
{
private static String fileName = "tworeads.xml";
public static void main(String[] args)
{
XmlTextReader reader = new XmlTextReader(fileName);
reader.set_WhitespaceHandling(WhitespaceHandling.None);
// Read the first part of the XML document
while (reader.Read()) {
// Display the elements and stop reading on the book endelement tag
// then go to ReadPart2 to start another reader to read the rest
// of the file.
switch (reader.get_NodeType()) {
case XmlNodeType.Element :
Console.WriteLine("Name: {0}", reader.get_Name());
break;
case XmlNodeType.Text :
Console.WriteLine(" Element Text: {0}", reader.get_Value());
break;
case XmlNodeType.EndElement :
// Stop reading when the reader gets to the end element
//of the book node.
if ("book".Equals(reader.get_LocalName())) {
Console.WriteLine("End reading first book...");
Console.WriteLine();
}
break;
}
}
// Read the rest of the XML document
// Create a new reader to read the rest of the document.
XmlTextReader reader2 = new XmlTextReader(reader.GetRemainder());
while (reader2.Read()) {
switch (reader2.get_NodeType()) {
case XmlNodeType.Element :
Console.WriteLine("Name: {0}", reader2.get_Name());
break;
case XmlNodeType.Text :
Console.WriteLine
(" Element Text: {0}", reader2.get_Value());
break;
case XmlNodeType.EndElement :
// Stop reading when the reader gets to the end element
// of the book node.
if ("book".Equals(reader2.get_LocalName())) {
Console.WriteLine("End reading second book...");
}
break;
}
}
reader.Close();
reader2.Close();
} //main
}//End class Sample
Nell'esempio viene utilizzato il file di input tworeads.xml.
<?xml version="1.0" ?> <bookstore> <book> <title>Pride And Prejudice</title> <author>Jane Austen</author> </book> <book> <title>The Handmaid's Tale</title> <author>Margaret Atwood</author> </book> </bookstore>
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.
.NET Framework
Supportato in: 2.0 1.1 1.0.NET Compact Framework
Supportato in: 2.0 1.0Riferimenti
Classe XmlTextReaderMembri XmlTextReader
Spazio dei nomi System.Xml
Nota