XmlWriter.Close XmlWriter.Close XmlWriter.Close XmlWriter.Close Method

定義

派生クラスでオーバーライドされると、このストリームと基になるストリームを閉じます。When overridden in a derived class, closes this stream and the underlying stream.

public:
 virtual void Close();
public virtual void Close ();
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public Overridable Sub Close ()

例外

Close が呼び出された後に、追加出力を書き込む呼び出しが行われたか、この呼び出しの結果が無効な XML ドキュメントです。A call is made to write more output after Close has been called or the result of this call is an invalid XML document.

または-or-

先行の非同期操作が完了する前に、XmlWriter メソッドが呼び出されました。An XmlWriter method was called before a previous asynchronous operation finished. この場合、「非同期操作が既に実行されています」というメッセージと共に InvalidOperationException がスローされます。In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

次の例では、XML ノードを書き込みます。The following example writes an XML node.

#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
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 'Main 
End Class 'Sample

注釈

すべての要素または属性が開いたままに自動的に閉じられます。Any elements or attributes left open are automatically closed.

注意

使用すると、XmlWriterを呼び出すまで、XML、要素および属性を出力するメソッドは書き込まれません、Closeメソッド。When you use the XmlWriter methods to output XML, the elements and attributes will not be written until you call the Close method. 設定する、XmlWriter を使用している場合など、XmlDocument閉じるまで、 XmlWriter、書き込まれた要素とターゲット ドキュメントに属性を確認することはできません。For example, if you are using the XmlWriter to populate an XmlDocument, until you close the XmlWriter, you will not be able to observe the written elements and attributes in the target document.

適用対象