XmlWriterSettings Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
public ref class XmlWriterSettings sealed
public sealed class XmlWriterSettings
type XmlWriterSettings = class
Public NotInheritable Class XmlWriterSettings
- Inheritance
-
XmlWriterSettings
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>
The Create method is the preferred mechanism for obtaining XmlWriter instances. The Create method uses the XmlWriterSettings class to specify which features to implement in the XmlWriter object that is created.
참고
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. This ensures that the created XmlWriter object has the correct output settings.
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 | Whether to check that characters are in the legal XML character set, as defined by W3C. | true or false |
true |
ConformanceLevel | Whether to check that output is a well-formed XML 1.0 document or fragment. | ConformanceLevel.Document (document-level), Fragment (fragment-level), or Auto (auto-detection) | ConformanceLevel.Document (document-level conformance) |
WriteEndDocumentOnClose | Whether to add closing tags to all unclosed elements when the Close method is called. | true or false |
true |
To specify output format, use these properties:
Property | Specifies | Value | Default |
---|---|---|---|
Encoding | Text encoding to use. | System.Text.Encoding value | Encoding.UTF8 |
Indent | Whether to indent elements | true or false |
false (no indentation) |
IndentChars | Character 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 (carriage return, line feed) for non-Unix platforms, or \n (line feed) for Unix platforms |
NewLineHandling | How to handle newline characters. | System.Xml.NewLineHandling value: Entitize (normalize), Replace (replace), or None (leave unchanged) | Replace (replace with characters specified by NewLineChars) |
NewLineOnAttributes | Whether to write attributes on individual lines (has no effect when Indent is false ). |
true or false |
false |
OmitXmlDeclaration | Whether to write an XML declaration. | true or false |
false |
Xml |
Initializes a new instance of the XmlWriterSettings class. |
Async |
Gets or sets a value that indicates whether asynchronous XmlWriter methods can be used on a particular XmlWriter instance. |
Check |
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. |
Close |
Gets or sets a value indicating whether the XmlWriter should also close the underlying stream or TextWriter when the Close() method is called. |
Conformance |
Gets or sets the level of conformance that the XML writer checks the XML output for. |
Do |
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. |
Indent |
Gets or sets the character string to use when indenting. This setting is used when the Indent property is set to |
Namespace |
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. |
New |
Gets or sets the character string to use for line breaks. |
New |
Gets or sets a value indicating whether to normalize line breaks in the output. |
New |
Gets or sets a value indicating whether to write attributes on a new line. |
Omit |
Gets or sets a value indicating whether to omit an XML declaration. |
Output |
Gets the method used to serialize the XmlWriter output. |
Write |
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() |
Creates a copy of the XmlWriterSettings instance. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
Get |
Serves as the default hash function. (Inherited from Object) |
Get |
Gets the Type of the current instance. (Inherited from Object) |
Memberwise |
Creates a shallow copy of the current Object. (Inherited from Object) |
Reset() |
Resets the members of the settings class to their default values. |
To |
Returns a string that represents the current object. (Inherited from Object) |
제품 | 버전 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET 피드백
.NET은(는) 오픈 소스 프로젝트입니다. 다음 링크를 선택하여 피드백을 제공해 주세요.