WriteTo Method

XDocument.WriteTo Method (XmlWriter)


Write this document to an XmlWriter.

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

public override void WriteTo(
	XmlWriter writer


Type: System.Xml.XmlWriter

An XmlWriter into which this method will write.

The following example shows how to write an XDocument to an XmlWriter. Note that the example did not write an XML declaration.

StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (XmlWriter xw = XmlWriter.Create(sb, xws))
    XDocument doc = new XDocument(
        new XElement("Child",
            new XElement("GrandChild", "some content")


This example produces the following output:

  <GrandChild>some content</GrandChild>

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft