This documentation is archived and is not being maintained.

XmlTextReader.GetRemainder Method

Gets the remainder of the buffered XML.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public TextReader GetRemainder()

Return Value

Type: System.IO.TextReader
A TextReader containing the remainder of the buffered XML.

NoteNote:

In the .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the XmlReader.Create method. This allows you to take full advantage of the new features introduced in this release. For more information, see Creating XML Readers.

Because XmlTextReader does a buffered Read, it must be able to return the remainder of the unused buffer so that no data is lost. This allows protocols (such as multi-part MIME) to package XML in the same stream as other things.

After calling this method, EOF is set to true.

The following example reads the first part of an XML document and then uses GetRemainder to complete reading the document using a second reader.

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 <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml; 

int main() {

   String* filename = S"tworeads.xml";

   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(S"Name: {0}", reader->Name);
          break;
       case XmlNodeType::Text:
          Console::WriteLine(S"  Element Text: {0}", reader->Value);
          break;
       case XmlNodeType::EndElement:
          // Stop reading when the reader gets to the end element of the book node.
          if (S"book"->Equals(reader->LocalName)) {
             Console::WriteLine(S"End reading first book...");
             Console::WriteLine();      
             goto ReadPart2;
          }
          break;
      } 
   } 

   // Read the rest of the XML document
ReadPart2:
   Console::WriteLine(S"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(S"Name: {0}", reader2->Name);
           break;
        case XmlNodeType::Text:
           Console::WriteLine(S"  Element Text: {0}", reader2->Value);
           break;
        case XmlNodeType::EndElement:
           // Stop reading when the reader gets to the end element of the book node.
           if (S"book"->Equals(reader2->LocalName)) {
              Console::WriteLine(S"End reading second book...");
              goto Done;
           }
           break;
      }
   }

Done:
   Console::WriteLine(S"Done.");
   reader->Close(); 
   reader2->Close();
}

The example uses the input file 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: