次の方法で共有


XmlTextReader.GetRemainder メソッド

バッファ内の XML の剰余を取得します。

Public Function GetRemainder() As TextReader
[C#]
public TextReader GetRemainder();
[C++]
public: TextReader* GetRemainder();
[JScript]
public function GetRemainder() : TextReader;

戻り値

バッファ内の XML の剰余を格納している TextReader

解説

XmlTextReader はバッファ内の読み取りを実行するため、データが消失しないように、未使用のバッファの剰余を返すことができる必要があります。これにより、プロトコル (マルチパート MIME など) で XML を他のものと同じストリームにパッケージ化できます。

このメソッドを呼び出すと、 EOFtrue に設定されます。

使用例

[Visual Basic, C#, C++] XML ドキュメントの最初の部分を読み取り、次に GetRemainder を使用して、2 番目のリーダーを使用したドキュメントの読み取りを完了する例を次に示します。

 
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

[C#] 
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

[C++] 
#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();
}

[Visual Basic, C#, C++] この例では、 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>

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

XmlTextReader クラス | XmlTextReader メンバ | System.Xml 名前空間