Serializing to Files, TextWriters, and XmlWriters

You can serialize XML trees to a File, a TextWriter, or an XmlWriter.

You can serialize any XML component, including XDocument and XElement, to a string by using the ToString method.

If you want to suppress formatting when serializing to a string, you can use the XNode.ToString method.

The default behavior when serializing to a file is to format (indent) the resulting XML document. When you indent, the insignificant white space in the XML tree is not preserved. To serialize with formatting, use one of the overloads of the following methods that do not take SaveOptions as an argument:

If you want the option not to indent and to preserve the insignificant white space in the XML tree, use one of the overloads of the following methods that takes SaveOptions as an argument:

For examples, see the appropriate reference topic.

See also