語言

XmlWriter.WriteDocType(String, String, String, String) 方法

定義

當在衍生類別中覆寫時,會寫入帶有指定名稱及可選屬性的 DOCTYPE 宣告。

public:
 abstract void WriteDocType(System::String ^ name, System::String ^ pubid, System::String ^ sysid, System::String ^ subset);
public abstract void WriteDocType(string name, string pubid, string sysid, string subset);
public abstract void WriteDocType(string name, string? pubid, string? sysid, string? subset);
abstract member WriteDocType : string * string * string * string -> unit
Public MustOverride Sub WriteDocType (name As String, pubid As String, sysid As String, subset As String)

參數

name
String

DOCTYPE 的名稱。 這必須是非空的。

pubid
String

若非空,則會寫入 PUBLIC「pubid」「sysid」,其中 pubid 和 sysid 被替換為給定參數的值。

sysid
String

若 pubid 且 nullsysid 非空,則會寫入 SYSTEM 的「sysid」,其中 sysid 以該參數的值替換。

subset
String

若非空,則寫成 [subset],其中 subset 被替換為該參數的值。

例外狀況

此方法在 prolog 之外(根元素後)被呼叫。

-或-

在先前非同步操作結束前,會呼叫一個 XmlWriter 方法。 此時, InvalidOperationException 會拋出「非同步操作已進行中」的訊息。

若值為 , name 則會導致 XML 無效。

範例

以下範例撰寫一個代表書籍的 XML 檔案。

using System;
using System.IO;
using System.Xml;

public class Sample {

  private const string filename = "sampledata.xml";

  public static void Main() {

     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     XmlWriter writer = XmlWriter.Create(filename, settings);

     // Write the Processing Instruction 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 the 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();
  }
}
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Const filename As String = "sampledata.xml"
    
  Public Shared Sub Main()

     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     Dim writer As XmlWriter = XmlWriter.Create(filename, settings)
        
     ' Write the Processing Instruction 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 the 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()
        
    End Sub
End Class

備註

此方法不檢查 中 pubid或 sysidsubset的無效字元。 它也不會檢查內部子集是否為良構。

Important

它 XmlWriter 不會驗證傳遞給 WriteDocType 方法的資料。 你不應該把任意的資料交給這個方法。

關於此方法的非同步版本,請參見 WriteDocTypeAsync。

適用於