XmlWriterSettings 类

定义

指定在由 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
继承
XmlWriterSettings

示例

下面的示例创建一个 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 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方法是用于获取实例的首选机制 XmlWriterThe Create method is the preferred mechanism for obtaining XmlWriter instances. Create方法使用 XmlWriterSettings 类来指定要在创建的对象中实现哪些功能 XmlWriterThe 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:

propertiesProperty 指定Specifies Value 默认Default
CheckCharacters 是否检查字符是否符合 W3C 定义的合法 XML 字符集。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 调用方法时是否向所有未关闭的元素添加结束标记 CloseWhether 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:

propertiesProperty 指定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 trueCharacter string to use when indenting (used when Indent is set to true). 字符串String 两个空格Two spaces
NewLineChars 用于分行符的字符串。Character string to use for line breaks. 字符串String \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 (替换为) 指定的字符 NewLineCharsReplace (replace with characters specified by NewLineChars)
NewLineOnAttributes 是否在单独的行上写入属性 (在) 时不起作用 Indent falseWhether 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 类的新实例。Initializes a new instance of the XmlWriterSettings class.

属性

Async

获取或设置一个值,该值指示是否可对特定的 XmlWriter 实例使用异步 XmlWriter 方法。Gets or sets a value that indicates whether asynchronous XmlWriter methods can be used on a particular XmlWriter instance.

CheckCharacters

获取或设置一个值,该值指示是否应检查 XML 编写器以确保文档中的所有字符都符合 W3C XML 1.0 建议中的“2.2 字符”部分。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

获取或设置一个值,该值指示调用 Close() 方法时 XmlWriter 是否也应关闭基础流或 TextWriterGets or sets a value indicating whether the XmlWriter should also close the underlying stream or TextWriter when the Close() method is called.

ConformanceLevel

获取或设置的 XML 写入器检查 XML 输出的一致性级别。Gets or sets the level of conformance that the XML writer checks the XML output for.

DoNotEscapeUriAttributes

获取或设置一个值,该值指示 XmlWriter 是否未转义 URI 属性。Gets or sets a value that indicates whether the XmlWriter does not escape URI attributes.

Encoding

获取或设置要使用的文本编码的类型。Gets or sets the type of text encoding to use.

Indent

获取或设置指示是否缩进元素的值。Gets or sets a value indicating whether to indent elements.

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

获取或设置一个值,该值指示在写入 XML 内容时 XmlWriter 是否应移除重复的命名空间声明。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

获取或设置要用于换行符的字符串。Gets or sets the character string to use for line breaks.

NewLineHandling

获取或设置一个值,该值指示是否将输出中的换行符规范化。Gets or sets a value indicating whether to normalize line breaks in the output.

NewLineOnAttributes

获取或设置一个值,该值指示是否在新行上写入属性。Gets or sets a value indicating whether to write attributes on a new line.

OmitXmlDeclaration

获取或设置一个值,该值指示是否省略 XML 声明。Gets or sets a value indicating whether to omit an XML declaration.

OutputMethod

获取用于串行化 XmlWriter 输出的方法。Gets the method used to serialize the XmlWriter output.

WriteEndDocumentOnClose

获取或设置一个值,该值指示在调用 Close() 方法时 XmlWriter 是否会向所有未关闭的元素标记添加结束标记。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()

创建 XmlWriterSettings 实例的副本。Creates a copy of the XmlWriterSettings instance.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Reset()

将设置类的成员重置为各自的默认值。Resets the members of the settings class to their default values.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于