편집

다음을 통해 공유


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.

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

A XmlWriter that the XDocument will be written to.

Examples

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

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

End Using

Console.WriteLine(sb.ToString())

This example produces the following output:

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

See also

Applies to

Save(Stream)

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

Outputs this XDocument 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
Public Sub Save (stream As 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

Save(TextWriter)

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

Serialize this XDocument 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 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.

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());
Dim sb As StringBuilder = New StringBuilder()

Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

Dim tr As TextWriter = New StringWriter(sb)
doc.Save(tr)
Console.WriteLine(sb.ToString())

This example produces the following output:

<?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

Save(String)

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

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

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

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.

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("Root.xml");
Console.WriteLine(File.ReadAllText("Root.xml"));
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

doc.Save("Root.xml")
Console.WriteLine(File.ReadAllText("Root.xml"))

This example produces the following output:

<?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

Save(Stream, SaveOptions)

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

Outputs this XDocument 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
Public Sub Save (stream As Stream, options As SaveOptions)

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

Save(TextWriter, SaveOptions)

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

Serialize this XDocument 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 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.

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());
}
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

Dim sb1 As StringBuilder = New StringBuilder()

Using sr1 = New StringWriter(sb1)
    doc.Save(sr1, SaveOptions.None)
    Console.WriteLine(sb1.ToString())
End Using

Dim sb2 As StringBuilder = New StringBuilder()

Using sr2 = New StringWriter(sb2)
    doc.Save(sr2, SaveOptions.DisableFormatting)
    Console.WriteLine(sb2.ToString())
End Using

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

Save(String, SaveOptions)

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

Serialize this XDocument 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

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.

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"));
Dim doc As XDocument = _
    <?xml version="1.0" encoding="utf-8"?>
        <Root><Child>content</Child></Root>

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