XmlTextWriter.WriteStartDocument 方法

定義

使用版本「1.0」寫入 XML 宣告。

多載

WriteStartDocument()

使用版本「1.0」寫入 XML 宣告。

WriteStartDocument(Boolean)

使用版本「1.0」寫入 XML 宣告與獨立屬性。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlWriter.Create 方法和 XmlWriterSettings 類別來建立 XmlWriter 實例,以利用新功能。

WriteStartDocument()

使用版本「1.0」寫入 XML 宣告。

public:
 override void WriteStartDocument();
public override void WriteStartDocument ();
override this.WriteStartDocument : unit -> unit
Public Overrides Sub WriteStartDocument ()

例外狀況

這不是在建構函式之後呼叫的第一個寫入方法。

範例

下列範例會寫入代表書籍的 XML 檔案。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextWriter^ writer = nullptr;
   String^ filename = "sampledata.xml";
   writer = gcnew XmlTextWriter( filename, nullptr );
   
   //Use indenting for readability.
   writer->Formatting = Formatting::Indented;
   
   //Write the XML delcaration. 
   writer->WriteStartDocument();
   
   //Write the ProcessingInstruction node.
   String^ PItext = "type='text/xsl' href='book.xsl'";
   writer->WriteProcessingInstruction( "xml-stylesheet", PItext );
   
   //Write the DocumentType node.
   writer->WriteDocType( "book", nullptr, nullptr, "<!ENTITY h 'hardcover'>" );
   
   //Write a Comment node.
   writer->WriteComment( "sample XML" );
   
   //Write a root element.
   writer->WriteStartElement( "book" );
   
   //Write the genre attribute.
   writer->WriteAttributeString( "genre", "novel" );
   
   //Write the ISBN attribute.
   writer->WriteAttributeString( "ISBN", "1-8630-014" );
   
   //Write the title.
   writer->WriteElementString( "title", "The Handmaid's Tale" );
   
   //Write the style element.
   writer->WriteStartElement( "style" );
   writer->WriteEntityRef( "h" );
   writer->WriteEndElement();
   
   //Write the price.
   writer->WriteElementString( "price", "19.95" );
   
   //Write CDATA.
   writer->WriteCData( "Prices 15% off!!" );
   
   //Write the close tag for the root element.
   writer->WriteEndElement();
   writer->WriteEndDocument();
   
   //Write the XML to file and close the writer.
   writer->Flush();
   writer->Close();
   
   //Load the file into an XmlDocument to ensure well formed XML.
   XmlDocument^ doc = gcnew XmlDocument;
   
   //Preserve white space for readability.
   doc->PreserveWhitespace = true;
   
   //Load the file.
   doc->Load( filename );
   
   //Display the XML content to the console.
   Console::Write( doc->InnerXml );
}
using System;
using System.IO;
using System.Xml;

public class Sample
{
  private const string filename = "sampledata.xml";

  public static void Main()
  {
     XmlTextWriter writer = null;

     writer = new XmlTextWriter (filename, null);
     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     //Write the XML delcaration.
     writer.WriteStartDocument();

     //Write the ProcessingInstruction node.
     String PItext="type='text/xsl' href='book.xsl'";
     writer.WriteProcessingInstruction("xml-stylesheet", PItext);

     //Write the DocumentType node.
     writer.WriteDocType("book", null , null, "<!ENTITY h 'hardcover'>");

     //Write a Comment node.
     writer.WriteComment("sample XML");

     //Write a root element.
     writer.WriteStartElement("book");

     //Write the genre attribute.
     writer.WriteAttributeString("genre", "novel");

     //Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014");

     //Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale");

     //Write the style element.
     writer.WriteStartElement("style");
     writer.WriteEntityRef("h");
     writer.WriteEndElement();

     //Write the price.
     writer.WriteElementString("price", "19.95");

     //Write CDATA.
     writer.WriteCData("Prices 15% off!!");

     //Write the close tag for the root element.
     writer.WriteEndElement();

     writer.WriteEndDocument();

     //Write the XML to file and close the writer.
     writer.Flush();
     writer.Close();

     //Load the file into an XmlDocument to ensure well formed XML.
     XmlDocument doc = new XmlDocument();
     //Preserve white space for readability.
     doc.PreserveWhitespace = true;
     //Load the file.
     doc.Load(filename);

     //Display the XML content to the console.
     Console.Write(doc.InnerXml);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared filename As String = "sampledata.xml"
    Public Shared Sub Main()
        Dim writer As XmlTextWriter = Nothing
        
        writer = New XmlTextWriter(filename, Nothing)
        'Use indenting for readability.
        writer.Formatting = Formatting.Indented
        
        'Write the XML delcaration. 
        writer.WriteStartDocument()
        
        'Write the ProcessingInstruction node.
        Dim PItext As String = "type='text/xsl' href='book.xsl'"
        writer.WriteProcessingInstruction("xml-stylesheet", PItext)
        
        'Write the DocumentType node.
        writer.WriteDocType("book", Nothing, Nothing, "<!ENTITY h 'hardcover'>")
        
        'Write a Comment node.
        writer.WriteComment("sample XML")
        
        'Write a root element.
        writer.WriteStartElement("book")
        
        'Write the genre attribute.
        writer.WriteAttributeString("genre", "novel")
        
        'Write the ISBN attribute.
        writer.WriteAttributeString("ISBN", "1-8630-014")
        
        'Write the title.
        writer.WriteElementString("title", "The Handmaid's Tale")
        
        'Write the style element.
        writer.WriteStartElement("style")
        writer.WriteEntityRef("h")
        writer.WriteEndElement()
        
        'Write the price.
        writer.WriteElementString("price", "19.95")
        
        'Write CDATA.
        writer.WriteCData("Prices 15% off!!")
        
        'Write the close tag for the root element.
        writer.WriteEndElement()
        
        writer.WriteEndDocument()
        
        'Write the XML to file and close the writer.
        writer.Flush()
        writer.Close()
        
        'Load the file into an XmlDocument to ensure well formed XML.
        Dim doc As New XmlDocument()
        'Preserve white space for readability.
        doc.PreserveWhitespace = True
        'Load the file.
        doc.Load(filename)
        
        'Display the XML content to the console.
        Console.Write(doc.InnerXml)
    End Sub
End Class

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlWriter.Create 方法和 XmlWriterSettings 類別來建立 XmlWriter 實例,以利用新功能。

檔的編碼層級取決於寫入器的實作方式。 例如,如果在建 Encoding 構函式中 XmlTextWriter 指定物件,這會決定編碼屬性的值。 這個方法不會建立獨立屬性。

呼叫 時 WriteStartDocument ,寫入器會驗證您正在撰寫的內容是格式正確的 XML 檔。 例如,它會檢查 XML 宣告是第一個節點,其中一個和只有一個根層級元素存在,依此類故。 如果未呼叫此方法,寫入器會假設正在寫入 XML 片段,而且不會套用根層級規則。

如果 WriteStartDocument 已呼叫 ,然後使用 WriteProcessingInstruction 方法來建立另一個 XML 宣告,則會擲回例外狀況。

適用於

WriteStartDocument(Boolean)

使用版本「1.0」寫入 XML 宣告與獨立屬性。

public:
 override void WriteStartDocument(bool standalone);
public override void WriteStartDocument (bool standalone);
override this.WriteStartDocument : bool -> unit
Public Overrides Sub WriteStartDocument (standalone As Boolean)

參數

standalone
Boolean

如果 true,它會寫入「standalone=yes」;如果 false,它會寫入「standalone=no」。

例外狀況

這不是在建構函式之後呼叫的第一個寫入方法。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlWriter.Create 方法和 XmlWriterSettings 類別來建立 XmlWriter 實例,以利用新功能。

檔的編碼層級取決於寫入器實作方式。 例如,如果在 Encoding 建構函式中 XmlTextWriter 指定物件,這會決定編碼屬性的值。

呼叫 時 WriteStartDocument ,寫入器會驗證您撰寫的內容是格式正確的 XML 檔。 例如,它會檢查 XML 宣告是第一個節點,其中一個和只有一個根層級元素存在等等。 如果未呼叫此方法,寫入器會假設正在寫入 XML 片段,而且不會套用根層級規則。

如果 WriteStartDocument 已呼叫 ,然後使用 WriteProcessingInstruction 方法來建立另一個 XML 宣告,則會擲回例外狀況。

適用於