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

属性值

Boolean

如果省略 XML 声明,则为 true;否则,为 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 设置为 DocumentXML 声明,则始终写入 XML 声明,即使 OmitXmlDeclaration 设置为 true

如果 ConformanceLevel 设置为 Fragment,则永远不会写入 XML 声明。 可以调用 WriteProcessingInstruction 显式写出 XML 声明。

适用于