XDocument.Save 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将此 XDocument 序列化为文件、TextWriter 或 XmlWriter。
重载
| Save(XmlWriter) | |
| Save(Stream) | |
| Save(TextWriter) | 将此 XDocument 序列化为 TextWriter。 | 
| Save(String) | 序列化此 XDocument 到文件,如果该文件存在,则覆盖现有的文件。 | 
| Save(Stream, SaveOptions) | |
| Save(TextWriter, SaveOptions) | 将此 XDocument 序列化为 TextWriter,还可以选择禁用格式设置。 | 
| Save(String, SaveOptions) | 将此 XDocument 序列化为文件,还可以选择禁用格式设置。 | 
Save(XmlWriter)
public:
 void Save(System::Xml::XmlWriter ^ writer);public void Save (System.Xml.XmlWriter writer);member this.Save : System.Xml.XmlWriter -> unitPublic Sub Save (writer As XmlWriter)参数
示例
下面的示例演示如何将一个 XDocument 保存到一个 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())  
该示例产生下面的输出:
<Child>  
  <GrandChild>some content</GrandChild>  
</Child>  
另请参阅
适用于
Save(Stream)
public:
 void Save(System::IO::Stream ^ stream);public void Save (System.IO.Stream stream);member this.Save : System.IO.Stream -> unitPublic Sub Save (stream As Stream)参数
注解
序列化的 XML 将缩进。 将删除所有微不足道的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该SaveOptions重载Save作为参数。 DisableFormatting使用此选项保存未输入的 XML。 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。
适用于
Save(TextWriter)
将此 XDocument 序列化为 TextWriter。
public:
 void Save(System::IO::TextWriter ^ textWriter);public void Save (System.IO.TextWriter textWriter);member this.Save : System.IO.TextWriter -> unitPublic Sub Save (textWriter As TextWriter)参数
- textWriter
- TextWriter
将向其中写入 TextWriter 的 XDocument。
示例
以下示例创建一个,将文档保存到一个XDocumentStringWriter,然后将字符串打印到控制台。
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())  
该示例产生下面的输出:
<?xml version="1.0" encoding="utf-16"?>  
<Root>  
  <Child>content</Child>  
</Root>  
注解
序列化的 XML 将缩进。 将删除所有微不足道的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该SaveOptions重载Save作为参数。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String)
序列化此 XDocument 到文件,如果该文件存在,则覆盖现有的文件。
public:
 void Save(System::String ^ fileName);public void Save (string fileName);member this.Save : string -> unitPublic Sub Save (fileName As String)参数
- fileName
- String
一个包含文件名称的字符串。
示例
以下示例创建一个 XDocument,将文档保存到文件,然后将该文件打印到控制台。
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"))  
该示例产生下面的输出:
<?xml version="1.0" encoding="utf-8"?>  
<Root>  
  <Child>content</Child>  
</Root>  
注解
序列化的 XML 将缩进。 将删除所有微不足道的空格,并添加额外的空格,以便正确缩进 XML。 此方法的行为是不会保留微不足道的空格。
如果要控制空格,请使用该SaveOptions重载Save作为参数。 有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(Stream, SaveOptions)
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 -> unitPublic Sub Save (stream As Stream, options As SaveOptions)参数
- options
- SaveOptions
指定格式设置行为的 SaveOptions。
注解
默认情况下,设置为 options None. 此选项将删除所有无关紧要的空白,并添加适当的不重要的空格,以便正确缩进 XML。
如果要保存未输入的 XML,请 DisableFormatting 指定其标志 options。 这将导致编写器编写所有空格完全如 XML 树中所示。
如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。
适用于
Save(TextWriter, SaveOptions)
将此 XDocument 序列化为 TextWriter,还可以选择禁用格式设置。
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 -> unitPublic Sub Save (textWriter As TextWriter, options As SaveOptions)参数
- textWriter
- TextWriter
要将 XML 输出到的 TextWriter。
- options
- SaveOptions
指定格式设置行为的 SaveOptions。
示例
以下示例演示此方法的两个用法。 第一个用法将序列化为 XDocument 格式设置。 第二个保留空格。 由于文档在构造时没有空格,因此保留空格会输出 XML,而无需任何缩进。
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  
该示例产生下面的输出:
<?xml version="1.0" encoding="utf-16"?>  
<Root>  
  <Child>content</Child>  
</Root>  
<?xml version="1.0" encoding="utf-16"?><Root><Child>content</Child></Root>  
注解
如果要保存未输入的 XML,请 DisableFormatting 指定其标志 options。 这将导致编写器完全如 XML 树中所示写入所有空白。
如果要保存缩进的 XML,请不要指定 DisableFormatting 该 options标志。 这将删除所有无关紧要的空白,并添加适当的不重要的空格,以便正确缩进 XML。 这是默认行为,以及不用作options参数的方法的Save重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。
另请参阅
适用于
Save(String, SaveOptions)
将此 XDocument 序列化为文件,还可以选择禁用格式设置。
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 -> unitPublic Sub Save (fileName As String, options As SaveOptions)参数
- fileName
- String
一个包含文件名称的字符串。
- options
- SaveOptions
指定格式设置行为的 SaveOptions。
示例
以下示例演示此方法的两个用法。 第一个用法保留空白。 第二个 XDocument 序列化为缩进。
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"))  
该示例产生下面的输出:
<?xml version="1.0" encoding="utf-8"?><Root><Child>content</Child></Root>  
<?xml version="1.0" encoding="utf-8"?>  
<Root>  
  <Child>content</Child>  
</Root>  
注解
如果要保存未输入的 XML,请 DisableFormatting 指定其标志 options。 这将导致编写器完全如 XML 树中所示写入所有空白。
如果要保存缩进的 XML,请不要指定 DisableFormatting 该 options标志。 这将删除所有无关紧要的空白,并添加适当的不重要的空格,以便正确缩进 XML。 这是默认行为,以及不用作options参数的方法的Save重载的行为。
有关详细信息,请参阅 在加载或分析 XML 时保留空白, 并在 序列化时保留空白。