XmlTextReader.GetRemainder Method

Gets the remainder of the buffered XML.

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

public TextReader GetRemainder ()
public TextReader GetRemainder ()
public function GetRemainder () : TextReader

Return Value

A TextReader containing the remainder of the buffered XML.

NoteNote

In the Microsoft .NET Framework version 2.0 release, the recommended practice is to create XmlReader instances using the System.Xml.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

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

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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: