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.

如果您想要控制空白字元,請使用 Save SaveOptions 做為參數的多載。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 讓您指定為參數的多載 SaveOptionsIf 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 Preserve white space while loading or parsing XML and Preserve 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 讓您指定為參數的多載 SaveOptionsIf 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 Preserve white space while loading or parsing XML and Preserve 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,請指定的 DisableFormatting 旗標 optionsIf 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,請指定的 DisableFormatting 旗標 optionsIf 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,請不要指定的 DisableFormatting 旗標 optionsIf 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. 這是預設行為,以及 Save 未採用做為參數之方法的多載行為 optionsThis 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 Preserve white space while loading or parsing XML and Preserve 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,請指定的 DisableFormatting 旗標 optionsIf 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,請不要指定的 DisableFormatting 旗標 optionsIf 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. 這是預設行為,以及 Save 未採用做為參數之方法的多載行為 optionsThis 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 Preserve white space while loading or parsing XML and Preserve white space while serializing.

適用於