XDocument.WriteTo Method (System.Xml.Linq)

Switch View :
ScriptFree
.NET Framework Class Library
XDocument.WriteTo Method

Write this document to an XmlWriter.

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

Visual Basic
Public Overrides Sub WriteTo ( _
	writer As XmlWriter _
)
C#
public override void WriteTo(
	XmlWriter writer
)
Visual C++
public:
virtual void WriteTo(
	XmlWriter^ writer
) override
F#
abstract WriteTo : 
        writer:XmlWriter -> unit 
override WriteTo : 
        writer:XmlWriter -> unit 

Parameters

writer
Type: System.Xml.XmlWriter
An XmlWriter into which this method will write.
Examples

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

C#
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")
        )
    );
    doc.WriteTo(xw);
}

Console.WriteLine(sb.ToString());
Visual Basic
Dim sb As StringBuilder = New StringBuilder()
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.OmitXmlDeclaration = True
xws.Indent = True

Using xw = XmlWriter.Create(sb, xws)
    Dim doc As XDocument = New XDocument(<Child><GrandChild>some content</GrandChild></Child>)
    doc.WriteTo(xw)
End Using

Console.WriteLine(sb.ToString())

This example produces the following output:

xmlLang
<Child>
  <GrandChild>some content</GrandChild>
</Child>
Version Information

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Reference

Other Resources