XElement.Save XElement.Save XElement.Save 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) Save(Stream) Save(Stream)

Outputs this XElement to the specified Stream.

Save(TextWriter) Save(TextWriter) Save(TextWriter)

Serialize this element to a TextWriter.

Save(String) Save(String) Save(String) Save(String)

Serialize this element to a file.

Save(XmlWriter) Save(XmlWriter) Save(XmlWriter) Save(XmlWriter)

Serialize this element to an XmlWriter.

Save(Stream, SaveOptions) Save(Stream, SaveOptions) Save(Stream, SaveOptions)

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

Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions)

Serialize this element to a TextWriter, optionally disabling formatting.

Save(String, SaveOptions) Save(String, SaveOptions) Save(String, SaveOptions) Save(String, SaveOptions)

Serialize this element to a file, optionally disabling formatting.

Save(Stream) Save(Stream) Save(Stream)

Outputs this XElement to the specified Stream.

public:
 void Save(System::IO::Stream ^ stream);
public void Save (System.IO.Stream stream);
member this.Save : System.IO.Stream -> unit
Parameters
stream
Stream Stream 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.

Save(TextWriter) Save(TextWriter) Save(TextWriter)

Serialize this element to a TextWriter.

public:
 void Save(System::IO::TextWriter ^ textWriter);
public void Save (System.IO.TextWriter textWriter);
member this.Save : System.IO.TextWriter -> unit
Parameters
textWriter
TextWriter TextWriter 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.

XElement root = XElement.Parse(@"<Root> <Child> Text </Child> </Root>");  
using (StringWriter sw = new StringWriter()) {  
    root.Save(sw);  
    Console.WriteLine(sw.ToString());  
}  
Dim root As XElement = <Root><Child> Text </Child></Root>  
Using sw = New StringWriter()  
    root.Save(sw)  
    Console.WriteLine(sw.ToString())  
End Using  

This example produces the following output:

<?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 Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

Save(String) Save(String) Save(String) Save(String)

Serialize this element to a file.

public:
 void Save(System::String ^ fileName);
public void Save (string fileName);
member this.Save : string -> unit
Public Sub Save (fileName As String)
Parameters
fileName
String String String 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.

XElement root = new XElement("Root",  
    new XElement("Child", "child content")  
);  
root.Save("Root.xml");  
string str = File.ReadAllText("Root.xml");  
Console.WriteLine(str);  
Dim root As XElement = _   
        <Root>  
            <Child>child content</Child>  
        </Root>  
root.Save("Root.xml")  
Dim Str As String = File.ReadAllText("Root.xml")  
Console.WriteLine(Str)  

This example produces the following output:

<?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 Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

Save(XmlWriter) Save(XmlWriter) Save(XmlWriter) Save(XmlWriter)

Serialize this element to an XmlWriter.

public:
 void Save(System::Xml::XmlWriter ^ writer);
public void Save (System.Xml.XmlWriter writer);
member this.Save : System.Xml.XmlWriter -> unit
Public Sub Save (writer As XmlWriter)
Parameters
writer
XmlWriter XmlWriter XmlWriter XmlWriter

A XmlWriter that the XElement will be written to.

Examples

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

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());  
Dim sb As StringBuilder = New StringBuilder()  
Dim xws As XmlWriterSettings = New XmlWriterSettings()  
xws.OmitXmlDeclaration = True  
Using xw = XmlWriter.Create(sb, xws)  
    Dim root As XElement = <Root>  
                               <Child>child content</Child>  
                           </Root>  
    root.Save(xw)  
End Using  
Console.WriteLine(sb.ToString())  

This example produces the following output:

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

Save(Stream, SaveOptions) Save(Stream, SaveOptions) Save(Stream, SaveOptions)

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

public:
 void Save(System::IO::Stream ^ stream, System::Xml::Linq::SaveOptions options);
public void Save (System.IO.Stream stream, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.Stream * System.Xml.Linq.SaveOptions -> unit
Parameters
stream
Stream Stream Stream Stream

The stream to output this XElement to.

options
SaveOptions SaveOptions SaveOptions 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.

Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions) Save(TextWriter, SaveOptions)

Serialize this element to a TextWriter, optionally disabling formatting.

public:
 void Save(System::IO::TextWriter ^ textWriter, System::Xml::Linq::SaveOptions options);
public void Save (System.IO.TextWriter textWriter, System.Xml.Linq.SaveOptions options);
member this.Save : System.IO.TextWriter * System.Xml.Linq.SaveOptions -> unit
Parameters
textWriter
TextWriter TextWriter TextWriter TextWriter

The TextWriter to output the XML to.

options
SaveOptions SaveOptions SaveOptions 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.

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());  
}  
Dim root As XElement = <Root><Child> Text </Child></Root>  

Using sw = New StringWriter()  
    root.Save(sw, SaveOptions.DisableFormatting)  
    Console.WriteLine(sw.ToString())  
End Using  

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

Using sw = New StringWriter()  
    root.Save(sw, SaveOptions.None)  
    Console.WriteLine(sw.ToString())  
End Using  

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 Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

Save(String, SaveOptions) Save(String, SaveOptions) Save(String, SaveOptions) Save(String, SaveOptions)

Serialize this element to a file, optionally disabling formatting.

public:
 void Save(System::String ^ fileName, System::Xml::Linq::SaveOptions options);
public void Save (string fileName, System.Xml.Linq.SaveOptions options);
member this.Save : string * System.Xml.Linq.SaveOptions -> unit
Public Sub Save (fileName As String, options As SaveOptions)
Parameters
fileName
String String String String

A String that contains the name of the file.

options
SaveOptions SaveOptions SaveOptions 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.

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);  
Dim str As String  
Dim root As XElement = <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 Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

Applies to