XElement.Save 方法

定义

序列化此元素的基础 XML 树。Serialize this element's underlying XML tree. 可以将输出保存到文件、XmlTextWriterTextWriterXmlWriterThe output can be saved to a file, an XmlTextWriter, a TextWriter, or an XmlWriter. 还可以禁用格式设置(缩进)。Optionally, formatting (indenting) can be disabled.

重载

Save(Stream)

将此 XElement 输出到指定的 StreamOutputs this XElement to the specified Stream.

Save(TextWriter)

将此元素序列化为 TextWriterSerialize this element to a TextWriter.

Save(String)

将此元素序列化为文件。Serialize this element to a file.

Save(XmlWriter)

将此元素序列化为 XmlWriterSerialize this element to an XmlWriter.

Save(Stream, SaveOptions)

将此 XElement 输出到指定的 Stream,(可选)并指定格式设置行为。Outputs this XElement to the specified Stream, optionally specifying formatting behavior.

Save(TextWriter, SaveOptions)

将此元素序列化为 TextWriter,并可以选择禁用格式设置。Serialize this element to a TextWriter, optionally disabling formatting.

Save(String, SaveOptions)

将此元素序列化为文件,并可以选择禁用格式设置。Serialize this element to a file, optionally disabling formatting.

Save(Stream)

将此 XElement 输出到指定的 StreamOutputs 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
Public Sub Save (stream As Stream)

参数

stream
Stream

将此 XElement 输出到的流。The stream to output this XElement to.

注解

将缩进序列化的 XML。The serialized XML will be indented. 将删除所有无意义的空白,并会添加额外的空白,以便正确缩进 XML。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.

如果要控制空白,请使用采用 SaveOptions 作为参数的 Save 的重载。If you want to control white space, use the overload of Save that takes SaveOptions as a parameter. 使用 DisableFormatting 选项可保存未缩进的 XML。Use the DisableFormatting option to save unindented XML. 这将导致编写器完全按 XML 树中的表示形式写入所有空格。This will cause the writer to write all white spaces exactly as represented in the XML tree.

如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。Use the OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Save(TextWriter)

将此元素序列化为 TextWriterSerialize 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)

参数

textWriter
TextWriter

将向其中写入 TextWriterXElementA TextWriter that the XElement will be written to.

示例

下面的示例创建一个 XElement,将文档保存到 StringWriter,然后将该字符串打印到控制台。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>  

注解

将缩进序列化的 XML。The serialized XML will be indented. 将删除所有无意义的空白,并会添加额外的空白,以便正确缩进 XML。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.

如果要控制空白,请使用 Save 的重载,以允许将 SaveOptions 指定为参数。If you want to control white space, use the overload of Save that allows you to specify SaveOptions as a parameter. 有关详细信息,请参阅在加载或分析 XML 时保留空白,并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

另请参阅

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)

参数

fileName
String

一个包含文件名称的 StringA String that contains the name of the file.

示例

下面的示例创建一个 XElement,将文档保存到文件,然后将文件打印到控制台。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>  

注解

将缩进序列化的 XML。The serialized XML will be indented. 将删除所有无意义的空白,并会添加额外的空白,以便正确缩进 XML。All insignificant white space will be removed, and additional white space will be added so that the XML will be properly indented. 此方法的行为是不会保留 XML 树中无意义的空白节点。The behavior of this method is that insignificant white space nodes in the XML tree will not be preserved.

如果要控制空白,请使用 Save 的重载,以允许将 SaveOptions 指定为参数。If you want to control white space, use the overload of Save that allows you to specify SaveOptions as a parameter. 有关详细信息,请参阅在加载或分析 XML 时保留空白,并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

另请参阅

Save(XmlWriter)

将此元素序列化为 XmlWriterSerialize 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)

参数

writer
XmlWriter

将向其中写入 XmlWriterXElementA XmlWriter that the XElement will be written to.

示例

下面的示例演示如何将一个 XElement 保存到一个 XmlWriterThe 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)

将此 XElement 输出到指定的 Stream,(可选)并指定格式设置行为。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
Public Sub Save (stream As Stream, options As SaveOptions)

参数

stream
Stream

将此 XElement 输出到的流。The stream to output this XElement to.

options
SaveOptions

一个 SaveOptions 对象,该对象指定格式设置行为。A SaveOptions object that specifies formatting behavior.

注解

默认情况下,options 设置为 NoneBy default the options are set to None. 此选项将删除所有无关的无意义空格,并添加适当的无意义空白,以便正确缩进 XML。This option will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented.

如果要保存未缩进的 XML,请为 options指定 DisableFormatting 标志。If you want to save unindented XML, specify the DisableFormatting flag for options. 这将导致编写器完全按 XML 树中的表示形式写入所有空格。This will cause the writer to write all white spaces exactly as represented in the XML tree.

如果要删除重复的命名空间声明,请使用 OmitDuplicateNamespaces 选项。Use OmitDuplicateNamespaces option if you want to remove duplicate namespace declarations.

Save(TextWriter, SaveOptions)

将此元素序列化为 TextWriter,并可以选择禁用格式设置。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)

参数

textWriter
TextWriter

要将 XML 输出到的 TextWriterThe TextWriter to output the XML to.

options
SaveOptions

指定格式设置行为的 SaveOptionsA SaveOptions that specifies formatting behavior.

示例

下面的示例演示了此方法的两种用法。The following example shows two uses of this method. 第一次使用将保留空白。The first use preserves white space. 第二个序列化 XElement 与格式设置。The second serializes the XElement with formatting. 由于文档在构造中没有空白,因此保留空白将输出 XML 而不进行任何缩进。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>  

注解

如果要保存未缩进的 XML,请为 options指定 DisableFormatting 标志。If you want to save unindented XML, specify the DisableFormatting flag for options. 这将导致编写器完全按 XML 树中的表示方式写入所有空格。This will cause the writer to write all white space exactly as represented in the XML tree.

如果要保存缩进的 XML,请不要为 options指定 DisableFormatting 标志。If you want to save indented XML, do not specify the DisableFormatting flag for options. 这将删除所有无关的无意义空格,并添加适当的无意义空白,以便正确缩进 XML。This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. 这是默认行为,以及不会将 options 为参数的 Save 方法的重载行为。This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

有关详细信息,请参阅在加载或分析 XML 时保留空白,并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

另请参阅

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)

参数

fileName
String

一个包含文件名称的 StringA String that contains the name of the file.

options
SaveOptions

指定格式设置行为的 SaveOptionsA SaveOptions that specifies formatting behavior.

示例

下面的示例演示了此方法的两种用法。The following example shows two uses of this method. 第一次使用将保留空白。The first use preserves white space. 第二个序列化 XElement 的格式设置。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>  

注解

如果要保存未缩进的 XML,请为 options指定 DisableFormatting 标志。If you want to save unindented XML, specify the DisableFormatting flag for options. 这将导致编写器完全按 XML 树中的表示方式写入所有空格。This will cause the writer to write all white space exactly as represented in the XML tree.

如果要保存缩进的 XML,请不要为 options指定 DisableFormatting 标志。If you want to save indented XML, do not specify the DisableFormatting flag for options. 这将删除所有无关的无意义空格,并添加适当的无意义空白,以便正确缩进 XML。This will remove all extraneous insignificant white space, and add appropriate insignificant white space so that the XML is properly indented. 这是默认行为,以及不会将 options 为参数的 Save 方法的重载行为。This is the default behavior, and the behavior of the overloads of the Save methods that do not take options as a parameter.

有关详细信息,请参阅在加载或分析 XML 时保留空白,并在序列化时保留空白区域For more information, see Preserving White Space while Loading or Parsing XML and Preserving White Space While Serializing.

另请参阅

适用于