XmlTextReader.GetRemainder 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得已緩衝之 XML 的其他部分。
public:
System::IO::TextReader ^ GetRemainder();
public System.IO.TextReader GetRemainder ();
member this.GetRemainder : unit -> System.IO.TextReader
Public Function GetRemainder () As TextReader
傳回
包含已緩衝的 XML 的其他部分的 TextReader。
範例
下列範例會讀取 XML 檔的第一個部分,然後使用 來使用 GetRemainder
第二個讀取器完成讀取檔。
#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();
}
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
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
End Class
此範例會使用輸入檔 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 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。
因為 XmlTextReader 會緩衝處理 Read
,所以必須能夠傳回未使用緩衝區的其餘部分,這樣就不會遺失任何資料。 這可讓通訊協定 (例如多部分 MIME) ,將 XML 封裝在與其他專案相同的資料流程中。
呼叫這個方法之後, EOF 會設定為 true
。