XmlWriter.Close 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,關閉這個資料流和基礎資料流。
public:
virtual void Close();
public:
abstract void Close();
public virtual void Close ();
public abstract void Close ();
abstract member Close : unit -> unit
override this.Close : unit -> unit
abstract member Close : unit -> unit
Public Overridable Sub Close ()
Public MustOverride Sub Close ()
例外狀況
在呼叫 Close
之後再進行呼叫以寫入更多輸出,或者這次呼叫的結果是無效的 XML 文件。
-或-
在先前的非同步作業完成前呼叫了 XmlWriter 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。
範例
下列範例會寫入 XML 節點。
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
// Create a writer to write XML to the console.
XmlWriterSettings^ settings = gcnew XmlWriterSettings;
settings->Indent = true;
settings->OmitXmlDeclaration = true;
XmlWriter^ writer = XmlWriter::Create( Console::Out, settings );
// Write the book element.
writer->WriteStartElement( L"book" );
// Write the title element.
writer->WriteStartElement( L"title" );
writer->WriteString( L"Pride And Prejudice" );
writer->WriteEndElement();
// Write the close tag for the root element.
writer->WriteEndElement();
// Write the XML and close the writer.
writer->Close();
return 1;
}
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
// Create a writer to write XML to the console.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(Console.Out, settings);
// Write the book element.
writer.WriteStartElement("book");
// Write the title element.
writer.WriteStartElement("title");
writer.WriteString("Pride And Prejudice");
writer.WriteEndElement();
// Write the close tag for the root element.
writer.WriteEndElement();
// Write the XML and close the writer.
writer.Close();
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
' Create a writer to write XML to the console.
Dim settings As XmlWriterSettings = new XmlWriterSettings()
settings.Indent = true
settings.OmitXmlDeclaration = true
Dim writer As XmlWriter = XmlWriter.Create(Console.Out, settings)
' Write the book element.
writer.WriteStartElement("book")
' Write the title element.
writer.WriteStartElement("title")
writer.WriteString("Pride And Prejudice")
writer.WriteEndElement()
' Write the close tag for the root element.
writer.WriteEndElement()
' Write the XML and close the writer.
writer.Close()
End Sub
End Class
備註
任何保持開啟的專案或屬性都會自動關閉。
注意
當您使用 XmlWriter 方法來輸出 XML 時,在您呼叫 Close 方法之前,將不會寫入元素和屬性。 例如,如果您使用 XmlWriter 來填 XmlDocument 入 ,直到您關閉 XmlWriter 為止,您將無法觀察目的檔案中的寫入專案和屬性。