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之前使用 XmlWriter 填充 XmlDocument,则将无法在目标文档中观察已写入的元素和属性。