共用方式為


XmlWriterSettings.OmitXmlDeclaration 屬性

定義

取得或設定值,指出是否省略 XML 宣告。

public:
 property bool OmitXmlDeclaration { bool get(); void set(bool value); };
public bool OmitXmlDeclaration { get; set; }
member this.OmitXmlDeclaration : bool with get, set
Public Property OmitXmlDeclaration As Boolean

屬性值

true 表示省略 XML 宣告,否則為 false。 預設值為 false,表示會寫入 XML 宣告。

範例

下列範例會將 XML 片段寫入記憶體資料流程。

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

// Create the XmlWriter object and write some content.
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm, settings);
writer.WriteElementString("orderID", "1-456-ab");
writer.WriteElementString("orderID", "2-36-00a");
writer.Flush();
writer.Close();

// Do additional processing on the stream.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.OmitXmlDeclaration = true
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.CloseOutput = false

' Create the XmlWriter object and write some content.
Dim strm as MemoryStream = new MemoryStream()
Dim writer As XmlWriter = XmlWriter.Create(strm, settings)
writer.WriteElementString("orderID", "1-456-ab")
writer.WriteElementString("orderID", "2-36-00a")
writer.Flush()
writer.Close()

' Do additional processing on the stream.

備註

此屬性只適用于 XmlWriter 輸出文字內容的實例,否則會忽略此設定。

如果 OmitXmlDeclaration 設定為 false ,則會自動寫入 XML 宣告

如果 ConformanceLevel 設定為 Document ,則一律會寫入 XML 宣告,即使 OmitXmlDeclaration 設定為 true 也一樣。

如果 ConformanceLevel 設定為 Fragment ,則永遠不會寫入 XML 宣告。 您可以呼叫 WriteProcessingInstruction 以明確寫出 XML 宣告。

適用於