XElement.Save Method

Definition

Serialize this element's underlying XML tree. The output can be saved to a file, an XmlTextWriter, a TextWriter, or an XmlWriter. Optionally, formatting (indenting) can be disabled.

Overloads

Save(Stream)

Outputs this XElement to the specified Stream.

Save(TextWriter)

Serialize this element to a TextWriter.

Save(String)

Serialize this element to a file.

Save(XmlWriter)

Serialize this element to an XmlWriter.

Save(Stream, SaveOptions)

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

Save(TextWriter, SaveOptions)

Serialize this element to a TextWriter, optionally disabling formatting.

Save(String, SaveOptions)

Serialize this element to a file, optionally disabling formatting.

Save(Stream)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Outputs this XElement to the specified Stream.

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

Parameters

stream
Stream

The stream to output this XElement 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 the OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Applies to

.NET 10 and other versions
Product Versions
.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:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Serialize this element to a TextWriter.

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

Parameters

textWriter
TextWriter

A TextWriter that the XElement will be written to.

Examples

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

C#
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");
using (StringWriter sw = new StringWriter()) {
    root.Save(sw);
    Console.WriteLine(sw.ToString());
}

This example produces the following output:

XML
<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child> Text </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 white space will not be preserved.

If you want to control white space, use the overload of Save that allows you to specify 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 and other versions
Product Versions
.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:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Serialize this element to a file.

C#
public void Save(string fileName);

Parameters

fileName
String

A String that contains the name of the file.

Examples

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

C#
XElement root = new XElement("Root",
    new XElement("Child", "child content")
);
root.Save("Root.xml");
string str = File.ReadAllText("Root.xml");
Console.WriteLine(str);

This example produces the following output:

XML
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child>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 nodes in the XML tree will not be preserved.

If you want to control white space, use the overload of Save that allows you to specify 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 and other versions
Product Versions
.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(XmlWriter)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Serialize this element to an XmlWriter.

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

Parameters

writer
XmlWriter

A XmlWriter that the XElement will be written to.

Examples

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

C#
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
    XElement root = new XElement("Root",
        new XElement("Child", "child content")
    );
    root.Save(xw);
}
Console.WriteLine(sb.ToString());

This example produces the following output:

XML
<Root><Child>child content</Child></Root>

See also

Applies to

.NET 10 and other versions
Product Versions
.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, SaveOptions)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Outputs this XElement 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 XElement to.

options
SaveOptions

A SaveOptions object 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 and other versions
Product Versions
.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:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Serialize this element 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 preserves white space. The second serializes the XElement with formatting. Because the document has no white space in it as constructed, preserving white space outputs the XML without any indenting.

C#
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");

using (StringWriter sw = new StringWriter())
{
    root.Save(sw, SaveOptions.DisableFormatting);
    Console.WriteLine(sw.ToString());
}

Console.WriteLine("=====");

using (StringWriter sw = new StringWriter())
{
    root.Save(sw, SaveOptions.None);
    Console.WriteLine(sw.ToString());
}

This example produces the following output:

<?xml version="1.0" encoding="utf-16"?><Root><Child> Text </Child></Root>
=====
<?xml version="1.0" encoding="utf-16"?>
<Root>
  <Child> Text </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 and other versions
Product Versions
.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:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Serialize this element 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 XElement with formatting.

C#
string str;
XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");

root.Save("Root.xml", SaveOptions.DisableFormatting);
str = File.ReadAllText("Root.xml");
Console.WriteLine(str);

Console.WriteLine("=====");

root.Save("Root.xml", SaveOptions.None);
str = File.ReadAllText("Root.xml");
Console.WriteLine(str);

This example produces the following output:

<?xml version="1.0" encoding="utf-8"?><Root><Child> Text </Child></Root>
=====
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Child> Text </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 and other versions
Product Versions
.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