XmlWriterSettings Class

Specifies a set of features to support on the XmlWriter object created by the XmlWriter.Create method.

System.Object
  System.Xml.XmlWriterSettings

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)

[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public sealed class XmlWriterSettings

The XmlWriterSettings type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsXmlWriterSettingsInitializes a new instance of the XmlWriterSettings class.
Top

  NameDescription
Public propertySupported in .NET for Windows Store appsAsyncGets or sets a value that indicates whether asynchronous XmlWriter methods can be used on a particular XmlWriter instance.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCheckCharactersGets 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.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCloseOutputGets or sets a value indicating whether the XmlWriter should also close the underlying stream or TextWriter when the Close method is called.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsConformanceLevelGets or sets the level of conformance that the XML writer checks the XML output for.
Public propertyDoNotEscapeUriAttributesGets or sets a value that indicates whether the XmlWriter do not escape URI attributes.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEncodingGets or sets the type of text encoding to use.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIndentGets or sets a value indicating whether to indent elements.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsIndentCharsGets or sets the character string to use when indenting. This setting is used when the Indent property is set to true.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsNamespaceHandlingGets 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.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNewLineCharsGets or sets the character string to use for line breaks.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNewLineHandlingGets or sets a value indicating whether to normalize line breaks in the output.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNewLineOnAttributesGets or sets a value indicating whether to write attributes on a new line.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOmitXmlDeclarationGets or sets a value indicating whether to omit an XML declaration.
Public propertySupported by the XNA FrameworkOutputMethodGets the method used to serialize the XmlWriter output.
Public propertySupported in .NET for Windows Store appsWriteEndDocumentOnCloseGets or sets a value that indicates whether the XmlWriter will add closing tags to all unclosed element tags when the Close method is called.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCloneCreates a copy of the XmlWriterSettings instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsResetResets the members of the settings class to their default values.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

NoteNote

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)

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

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();

The sample produces the following output:

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft