XDocument.Save Method

Definition

Serializes this XDocument to a file, a TextWriter, or an XmlWriter.

Overloads

Save(XmlWriter)

Serialize this XDocument to an XmlWriter.

Save(Stream)

Outputs this XDocument to the specified Stream.

Save(TextWriter)

Serialize this XDocument to a TextWriter.

Save(String)

Serialize this XDocument to a file, overwriting an existing file, if it exists.

Save(Stream, SaveOptions)

Outputs this XDocument to the specified Stream, optionally specifying formatting behavior.

Save(TextWriter, SaveOptions)

Serialize this XDocument to a TextWriter, optionally disabling formatting.

Save(String, SaveOptions)

Serialize this XDocument to a file, optionally disabling formatting.

Save(XmlWriter)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Serialize this XDocument to an XmlWriter.

C#
public void Save(System.Xml.XmlWriter writer);

Parameters

writer
XmlWriter

A XmlWriter that the XDocument will be written to.

Examples

The following example shows how to save an XDocument to an XmlWriter.

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.Save(xw);
}

Console.WriteLine(sb.ToString());

This example produces the following output:

XML
<Child>
  <GrandChild>some content</GrandChild>
</Child>

See also

Applies to

.NET 10 i inne wersje
Produkt Wersje
.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, 10
.NET Framework 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

Save(Stream)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Outputs this XDocument to the specified Stream.

C#
public void Save(System.IO.Stream stream);

Parameters

stream
Stream

The stream to output this XDocument to.

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space will not be preserved.

If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. Use the DisableFormatting option to save unindented XML. This will cause the writer to write all white spaces exactly as represented in the XML tree.

Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Applies to

.NET 10 i inne wersje
Produkt Wersje
.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, 10
.NET Framework 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

Save(TextWriter)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Serialize this XDocument to a TextWriter.

C#
public void Save(System.IO.TextWriter textWriter);

Parameters

textWriter
TextWriter

A TextWriter that the XDocument will be written to.

Examples

The following example creates an XDocument, saves the document to a StringWriter, and then prints the string to the console.

C#
StringBuilder sb = new StringBuilder();

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
TextWriter tr = new StringWriter(sb);
doc.Save(tr);
Console.WriteLine(sb.ToString());

This example produces the following output:

XML
<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child>content</Child>
</Root>

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space will not be preserved.

If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

.NET 10 i inne wersje
Produkt Wersje
.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, 10
.NET Framework 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

Save(String)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Serialize this XDocument to a file, overwriting an existing file, if it exists.

C#
public void Save(string fileName);

Parameters

fileName
String

A string that contains the name of the file.

Examples

The following example creates an XDocument, saves the document to a file, and then prints the file to the console.

C#
XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("Root.xml");
Console.WriteLine(File.ReadAllText("Root.xml"));

This example produces the following output:

XML
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child>content</Child>
</Root>

Remarks

The serialized XML will be indented. All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. The behavior of this method is that insignificant white space will not be preserved.

If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

.NET 10 i inne wersje
Produkt Wersje
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 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 2.0, 2.1

Save(Stream, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Outputs this XDocument to the specified Stream, optionally specifying formatting behavior.

C#
public void Save(System.IO.Stream stream, System.Xml.Linq.SaveOptions options);

Parameters

stream
Stream

The stream to output this XDocument to.

options
SaveOptions

A SaveOptions that specifies formatting behavior.

Remarks

By default the options are set to None. This option will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented.

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white spaces exactly as represented in the XML tree.

Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Applies to

.NET 10 i inne wersje
Produkt Wersje
.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, 10
.NET Framework 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

Save(TextWriter, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Serialize this XDocument to a TextWriter, optionally disabling formatting.

C#
public void Save(System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options);

Parameters

textWriter
TextWriter

The TextWriter to output the XML to.

options
SaveOptions

A SaveOptions that specifies formatting behavior.

Examples

The following example shows two uses of this method. The first use serializes the XDocument with formatting. The second preserves white space. Because the document has no white space in it as constructed, preserving white space outputs the XML without any indenting.

C#
XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
StringBuilder sb1 = new StringBuilder();
using (StringWriter sr1 = new StringWriter(sb1)) {
    doc.Save(sr1, SaveOptions.None);
    Console.WriteLine(sb1.ToString());
}

StringBuilder sb2 = new StringBuilder();
using (StringWriter sr2 = new StringWriter(sb2)) {
    doc.Save(sr2, SaveOptions.DisableFormatting);
    Console.WriteLine(sb2.ToString());
}

This example produces the following output:

<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child>content</Child>
</Root>
<?xml version="1.0" encoding="utf-16"?><Root><Child>content</Child></Root>

Remarks

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white space exactly as represented in the XML tree.

If you want to save indented XML, do not specify the DisableFormatting flag for options. This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

.NET 10 i inne wersje
Produkt Wersje
.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, 10
.NET Framework 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

Save(String, SaveOptions)

Source:
XDocument.cs
Source:
XDocument.cs
Source:
XDocument.cs

Serialize this XDocument to a file, optionally disabling formatting.

C#
public void Save(string fileName, System.Xml.Linq.SaveOptions options);

Parameters

fileName
String

A string that contains the name of the file.

options
SaveOptions

A SaveOptions that specifies formatting behavior.

Examples

The following example shows two uses of this method. The first use preserves white space. The second one serializes the XDocument with indenting.

C#
XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("Root1.xml", SaveOptions.DisableFormatting);
Console.WriteLine(File.ReadAllText("Root1.xml"));
doc.Save("Root2.xml", SaveOptions.None);
Console.WriteLine(File.ReadAllText("Root2.xml"));

This example produces the following output:

<?xml version="1.0" encoding="utf-8"?><Root><Child>content</Child></Root>
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child>content</Child>
</Root>

Remarks

If you want to save unindented XML, specify the DisableFormatting flag for options. This will cause the writer to write all white space exactly as represented in the XML tree.

If you want to save indented XML, do not specify the DisableFormatting flag for options. This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

For more information, see Preserve white space while loading or parsing XML and Preserve white space while serializing.

See also

Applies to

.NET 10 i inne wersje
Produkt Wersje
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 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 2.0, 2.1