XmlWriterSettings XmlWriterSettings XmlWriterSettings XmlWriterSettings Class

定義

指定要在由 XmlWriter 方法建立的 Create 物件上支援的一組功能。Specifies a set of features to support on the XmlWriter object created by the Create method.

public ref class XmlWriterSettings sealed
public sealed class XmlWriterSettings
type XmlWriterSettings = class
Public NotInheritable Class XmlWriterSettings
繼承
XmlWriterSettingsXmlWriterSettingsXmlWriterSettingsXmlWriterSettings

範例

下列範例會建立XmlWriter,寫入至 XML 檔案,並寫入新行上的每個屬性。The following example creates an XmlWriter that writes to an XML file and writes each attribute on a new line.

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
settings.NewLineOnAttributes = true;
       
writer = XmlWriter.Create(Console.Out, settings);

writer.WriteStartElement("order");
writer.WriteAttributeString("orderID", "367A54");
writer.WriteAttributeString("date", "2001-05-03");
writer.WriteElementString("price", "19.95");
writer.WriteEndElement();
	
writer.Flush();
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = true
settings.OmitXmlDeclaration = true
settings.NewLineOnAttributes = true
       
writer = XmlWriter.Create(Console.Out, settings)

writer.WriteStartElement("order")
writer.WriteAttributeString("orderID", "367A54")
writer.WriteAttributeString("date", "2001-05-03")
writer.WriteElementString("price", "19.95")
writer.WriteEndElement()
	
writer.Flush()

此範例會產生下列輸出:The sample produces the following output:

<order  
  orderID="367A54"  
  date="2001-05-03">  
  <price>19.95</price>  
</order>  

備註

Create方法是慣用的機制,以取得XmlWriter執行個體。The Create method is the preferred mechanism for obtaining XmlWriter instances. Create方法會使用XmlWriterSettings類別,以指定要在實作哪些功能XmlWriter所建立的物件。The Create method uses the XmlWriterSettings class to specify which features to implement in the XmlWriter object that is created.

注意

如果您使用XmlWriter物件Transform方法,您應該使用OutputSettings屬性,以取得XmlWriterSettings具有正確的設定物件。If you're using the XmlWriter object with the Transform method, you should use the OutputSettings property to obtain an XmlWriterSettings object with the correct settings. 如此可確保所建立的 XmlWriter 物件具有正確的輸出設定。This ensures that the created XmlWriter object has the correct output settings.

XmlWriterSettings類別提供屬性,控制資料一致性和輸出格式。The XmlWriterSettings class provides properties that control data conformance and output format.

資料一致性檢查和自動更正,請使用這些屬性:For data conformance checks and auto-corrections, use these properties:

屬性Property 指定Specifies Value 預設Default
CheckCharacters 是否要檢查的字元會在合法的 XML 字元集,如 W3C 所定義。Whether to check that characters are in the legal XML character set, as defined by W3C. truefalsetrue or false true
ConformanceLevel 是否要檢查輸出是否格式正確的 XML 1.0 文件或片段。Whether to check that output is a well-formed XML 1.0 document or fragment. ConformanceLevel.Document (文件層級)、 Fragment (片段層級),或Auto(自動偵測)ConformanceLevel.Document (document-level), Fragment (fragment-level), or Auto (auto-detection) ConformanceLevel.Document (文件層級的一致性)ConformanceLevel.Document (document-level conformance)
WriteEndDocumentOnClose 是否要將結尾標記加入至所有未封閉的項目時Close呼叫方法。Whether to add closing tags to all unclosed elements when the Close method is called. truefalsetrue or false true

若要指定輸出格式,請使用這些屬性:To specify output format, use these properties:

屬性Property 指定Specifies Value 預設Default
Encoding 若要使用的編碼方式的文字。Text encoding to use. System.Text.EncodingSystem.Text.Encoding value Encoding.UTF8
Indent 是否要縮排項目Whether to indent elements truefalsetrue or false false (沒有縮排)false (no indentation)
IndentChars 字元縮排時使用的字串 (使用的時機Indent設為true)。Character string to use when indenting (used when Indent is set to true). StringString 兩個空格Two spaces
NewLineChars 要用於分行符號的字元字串。Character string to use for line breaks. StringString \r\n (歸位字元、 換行字元)\r\n (carriage return, line feed)
NewLineHandling 如何處理新行字元。How to handle newline characters. System.Xml.NewLineHandling 值: Entitize (標準化) Replace (取代),或None(保持不變)System.Xml.NewLineHandling value: Entitize (normalize), Replace (replace), or None (leave unchanged) Replace (所指定的字元取代NewLineChars)Replace (replace with characters specified by NewLineChars)
NewLineOnAttributes 是否要在個別行上寫入屬性 (不含任何時生效Indentfalse)。Whether to write attributes on individual lines (has no effect when Indent is false). truefalsetrue or false false
OmitXmlDeclaration 是否要寫入 XML 宣告。Whether to write an XML declaration. truefalsetrue or false false

建構函式

XmlWriterSettings() XmlWriterSettings() XmlWriterSettings() XmlWriterSettings()

初始化 XmlWriterSettings 類別的新執行個體。Initializes a new instance of the XmlWriterSettings class.

屬性

Async Async Async Async

取得或設定值,指出非同步 XmlWriter 方法是否可以用於特定 XmlWriter 執行個體。Gets or sets a value that indicates whether asynchronous XmlWriter methods can be used on a particular XmlWriter instance.

CheckCharacters CheckCharacters CheckCharacters CheckCharacters

取得或設定值,這個值表示 XML 寫入器是否應該檢查以確定文件中的所有字元都符合 W3C XML 1.0 Recommendation 中的 "2.2 Characters" 一節。Gets or sets a value that indicates whether the XML writer should check to ensure that all characters in the document conform to the "2.2 Characters" section of the W3C XML 1.0 Recommendation.

CloseOutput CloseOutput CloseOutput CloseOutput

取得或設定值,指出呼叫 XmlWriter 方法時,TextWriter 是否也應該關閉基礎資料流或 Close()Gets or sets a value indicating whether the XmlWriter should also close the underlying stream or TextWriter when the Close() method is called.

ConformanceLevel ConformanceLevel ConformanceLevel ConformanceLevel

取得或設定 XML 寫入器檢查 XML 輸出是否符合的一致性層級。Gets or sets the level of conformance that the XML writer checks the XML output for.

DoNotEscapeUriAttributes DoNotEscapeUriAttributes DoNotEscapeUriAttributes DoNotEscapeUriAttributes

取得或設定值,這個值會指出 XmlWriter 是否不會逸出 URI 屬性。Gets or sets a value that indicates whether the XmlWriter does not escape URI attributes.

Encoding Encoding Encoding Encoding

取得或設定要使用的文字編碼方式類型。Gets or sets the type of text encoding to use.

Indent Indent Indent Indent

取得或設定值,指出是否要縮排項目。Gets or sets a value indicating whether to indent elements.

IndentChars IndentChars IndentChars IndentChars

取得或設定縮排時使用的字元字串。Gets or sets the character string to use when indenting. Indent 屬性設為 true 時會使用這項設定。This setting is used when the Indent property is set to true.

NamespaceHandling NamespaceHandling NamespaceHandling NamespaceHandling

取得或設定值,這個值表示 XmlWriter 是否應該在寫入 XML 內容時移除重複的命名空間宣告。Gets or sets a value that indicates whether the XmlWriter should remove duplicate namespace declarations when writing XML content. 預設行為是讓寫入器輸出寫入器命名空間解析程式中出現的所有命名空間宣告。The default behavior is for the writer to output all namespace declarations that are present in the writer's namespace resolver.

NewLineChars NewLineChars NewLineChars NewLineChars

取得或設定用於分行符號的字元字串。Gets or sets the character string to use for line breaks.

NewLineHandling NewLineHandling NewLineHandling NewLineHandling

取得或設定值,指出是否要將輸出中的分行符號正規化。Gets or sets a value indicating whether to normalize line breaks in the output.

NewLineOnAttributes NewLineOnAttributes NewLineOnAttributes NewLineOnAttributes

取得或設定值,指出是否將屬性寫在新行上。Gets or sets a value indicating whether to write attributes on a new line.

OmitXmlDeclaration OmitXmlDeclaration OmitXmlDeclaration OmitXmlDeclaration

取得或設定值,指出是否省略 XML 宣告。Gets or sets a value indicating whether to omit an XML declaration.

OutputMethod OutputMethod OutputMethod OutputMethod

取得用於序列化 XmlWriter 輸出的方法。Gets the method used to serialize the XmlWriter output.

WriteEndDocumentOnClose WriteEndDocumentOnClose WriteEndDocumentOnClose WriteEndDocumentOnClose

取得或設定值,指出 XmlWriter 是否會在呼叫 Close() 方法時,將結尾標記加入至所有未封閉的項目標記。Gets or sets a value that indicates whether the XmlWriter will add closing tags to all unclosed element tags when the Close() method is called.

方法

Clone() Clone() Clone() Clone()

建立 XmlWriterSettings 執行個體的複本。Creates a copy of the XmlWriterSettings instance.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Reset() Reset() Reset() Reset()

將設定類別的成員重設為其預設值。Resets the members of the settings class to their default values.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於