XmlTextWriter.WriteStartDocument Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menulis deklarasi XML dengan versi "1.0".
Overload
| Nama | Deskripsi |
|---|---|
| WriteStartDocument() |
Menulis deklarasi XML dengan versi "1.0". |
| WriteStartDocument(Boolean) |
Menulis deklarasi XML dengan versi "1.0" dan atribut mandiri. |
Keterangan
Note
Kami menyarankan agar Anda membuat XmlWriter instans dengan menggunakan XmlWriter.Create metode dan XmlWriterSettings kelas untuk memanfaatkan fungsionalitas baru.
WriteStartDocument()
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
Menulis deklarasi XML dengan versi "1.0".
public:
override void WriteStartDocument();
public override void WriteStartDocument();
override this.WriteStartDocument : unit -> unit
Public Overrides Sub WriteStartDocument ()
Pengecualian
Ini bukan metode tulis pertama yang dipanggil setelah konstruktor.
Contoh
Contoh berikut menulis file XML yang mewakili buku.
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
Keterangan
Note
Kami menyarankan agar Anda membuat XmlWriter instans dengan menggunakan XmlWriter.Create metode dan XmlWriterSettings kelas untuk memanfaatkan fungsionalitas baru.
Tingkat pengodean dokumen ditentukan oleh bagaimana penulis diimplementasikan. Misalnya, jika Encoding objek ditentukan dalam XmlTextWriter konstruktor, ini menentukan nilai atribut pengodean. Metode ini tidak membuat atribut mandiri.
Ketika WriteStartDocument disebut penulis memvalidasi bahwa apa yang Anda tulis adalah dokumen XML yang terbentuk dengan baik. Misalnya, ia memeriksa bahwa deklarasi XML adalah simpul pertama, bahwa satu dan hanya satu elemen tingkat akar yang ada dan sebagainya. Jika metode ini tidak dipanggil, penulis mengasumsikan fragmen XML sedang ditulis dan tidak menerapkan aturan tingkat akar.
Jika WriteStartDocument telah dipanggil dan kemudian WriteProcessingInstruction metode digunakan untuk membuat deklarasi XML lain, pengecualian akan dilemparkan.
Berlaku untuk
WriteStartDocument(Boolean)
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
- Sumber:
- XmlTextWriter.cs
Menulis deklarasi XML dengan versi "1.0" dan atribut mandiri.
public:
override void WriteStartDocument(bool standalone);
public override void WriteStartDocument(bool standalone);
override this.WriteStartDocument : bool -> unit
Public Overrides Sub WriteStartDocument (standalone As Boolean)
Parameter
- standalone
- Boolean
Jika true, ia menulis "standalone=yes"; jika false, ia menulis "standalone=no".
Pengecualian
Ini bukan metode tulis pertama yang dipanggil setelah konstruktor.
Keterangan
Note
Kami menyarankan agar Anda membuat XmlWriter instans dengan menggunakan XmlWriter.Create metode dan XmlWriterSettings kelas untuk memanfaatkan fungsionalitas baru.
Tingkat pengodean dokumen ditentukan oleh bagaimana penulis diimplementasikan. Misalnya, jika Encoding objek ditentukan dalam XmlTextWriter konstruktor, ini menentukan nilai atribut pengodean.
Ketika WriteStartDocument disebut penulis memvalidasi bahwa apa yang Anda tulis adalah dokumen XML yang terbentuk dengan baik. Misalnya, ia memeriksa bahwa deklarasi XML adalah simpul pertama, bahwa satu dan hanya satu elemen tingkat akar yang ada dan sebagainya. Jika metode ini tidak dipanggil, penulis mengasumsikan fragmen XML sedang ditulis dan tidak menerapkan aturan tingkat akar.
Jika WriteStartDocument telah dipanggil dan kemudian WriteProcessingInstruction metode digunakan untuk membuat deklarasi XML lain, pengecualian akan dilemparkan.