共用方式為


XmlWriter.Flush 方法

定義

在衍生類別中覆寫時,將緩衝區的所有內容清空至基礎資料流,然後清空基礎資料流。

public:
 abstract void Flush();
public abstract void Flush ();
abstract member Flush : unit -> unit
Public MustOverride Sub Flush ()

例外狀況

在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

範例

下列範例會寫入兩個 XML 片段。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
void main()
{
   
   // Create an XmlWriter to write XML fragments.
   XmlWriterSettings^ settings = gcnew XmlWriterSettings;
   settings->ConformanceLevel = ConformanceLevel::Fragment;
   settings->Indent = true;
   XmlWriter^ writer = XmlWriter::Create( Console::Out, settings );
   
   // Write an XML fragment.
   writer->WriteStartElement( L"book" );
   writer->WriteElementString( L"title", L"Pride And Prejudice" );
   writer->WriteEndElement();
   writer->Flush();
   
   // Write another XML fragment.
   writer->WriteStartElement( L"cd" );
   writer->WriteElementString( L"title", L"Americana" );
   writer->WriteEndElement();
   writer->Flush();
   
   // Close the writer.
   writer->Close();
}
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

     // Create an XmlWriter to write XML fragments.
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.ConformanceLevel = ConformanceLevel.Fragment;
     settings.Indent = true;
     XmlWriter writer = XmlWriter.Create(Console.Out, settings);

     // Write an XML fragment.
     writer.WriteStartElement("book");
     writer.WriteElementString("title", "Pride And Prejudice");
     writer.WriteEndElement();
     writer.Flush();

     // Write another XML fragment.
     writer.WriteStartElement("cd");
     writer.WriteElementString("title", "Americana");
     writer.WriteEndElement();
     writer.Flush();

     // Close the writer.
     writer.Close();
  }
}
Imports System.IO
Imports System.Xml

Public Class Sample

  Public Shared Sub Main()

     ' Create an XmlWriter to write XML fragments.
     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.ConformanceLevel = ConformanceLevel.Fragment
     settings.Indent = true
     Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
    
     ' Write an XML fragment.
     writer.WriteStartElement("book")
     writer.WriteElementString("title", "Pride And Prejudice")
     writer.WriteEndElement()
     writer.Flush()

     ' Write another XML fragment.
     writer.WriteStartElement("cd")
     writer.WriteElementString("title", "Americana")
     writer.WriteEndElement()
     writer.Flush()  

     'Close the writer.
     writer.Close()

  End Sub
End Class

備註

這會呼叫,而不是 Close 當您想要將更多內容寫入基礎資料流程時,而不會遺失仍在緩衝區中的內容。

如需這個方法的非同步版本,請參閱 FlushAsync

適用於