XElement.Save Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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(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
public:
void Save(System::IO::Stream ^ stream);
public void Save (System.IO.Stream stream);
member this.Save : System.IO.Stream -> unit
Public Sub Save (stream As Stream)
Parameters
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
Save(TextWriter)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
Public Sub Save (textWriter As 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.
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 Preserve white space while loading or parsing XML and Preserve white space while serializing.
See also
Applies to
Save(String)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
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 Preserve white space while loading or parsing XML and Preserve white space while serializing.
See also
Applies to
Save(XmlWriter)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
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>
See also
Applies to
Save(Stream, SaveOptions)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
Public Sub Save (stream As Stream, options As SaveOptions)
Parameters
- 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
Save(TextWriter, SaveOptions)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
Public Sub Save (textWriter As TextWriter, options As SaveOptions)
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.
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 Preserve white space while loading or parsing XML and Preserve white space while serializing.
See also
Applies to
Save(String, SaveOptions)
- Source:
- XElement.cs
- Source:
- XElement.cs
- Source:
- XElement.cs
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
- 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.
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 Preserve white space while loading or parsing XML and Preserve white space while serializing.
See also
Applies to
.NET