แก้ไข

แชร์ผ่าน


XmlWriterSettings.OmitXmlDeclaration Property

Definition

Gets or sets a value indicating whether to omit an XML declaration.

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

Property Value

true to omit the XML declaration; otherwise, false. The default is false, an XML declaration is written.

Examples

The following example writes an XML fragment to a memory stream.

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.

Remarks

This property only applies to XmlWriter instances that output text content; otherwise, this setting is ignored.

If OmitXmlDeclaration is set to false, The XML declaration is written automatically

The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true.

The XML declaration is never written if ConformanceLevel is set to Fragment. You can call WriteProcessingInstruction to explicitly write out an XML declaration.

Applies to