XmlTextReader.GetRemainder Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the remainder of the buffered XML.

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

Public Function GetRemainder As TextReader

Return Value

Type: System.IO.TextReader

A TextReader containing the remainder of the buffered XML.

System_CAPS_noteNote

Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

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.

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

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>

.NET Framework
Available since 1.1
Return to top
Show: