Share via


XDocument.Save 方法

定義

將此 XDocument 序列化為檔案、TextWriterXmlWriter

多載

Save(XmlWriter)

將此 XDocument 序列化為 XmlWriter

Save(Stream)

將這個 XDocument 輸出到指定的 Stream

Save(TextWriter)

將此 XDocument 序列化為 TextWriter

Save(String)

將此 XDocument 序列化至檔案,並覆寫現有的檔案 (如果存在)。

Save(Stream, SaveOptions)

將這個 XDocument 輸出至指定的 Stream,選擇性地指定格式化行為。

Save(TextWriter, SaveOptions)

將此 XDocument 序列化為 TextWriter,選擇性地停用格式設定。

Save(String, SaveOptions)

將此 XDocument 序列化為檔案,選擇性地停用格式設定。

Save(XmlWriter)

將此 XDocument 序列化為 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

向其中寫入 XmlWriterXDocument

範例

下列範例顯示如何將 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)

將這個 XDocument 輸出到指定的 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

這個 XDocument 輸出的目的資料流。

備註

序列化的 XML 將會縮排。 將會移除所有不重要的空白字元,並新增額外的空白字元,以便正確地縮排 XML。 這個方法的行為是不會保留不重要的空白字元。

如果您想要控制空白字元,請使用 採用 做為參數的 Save SaveOptions 多載。 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 -> unit
Public Sub Save (textWriter As TextWriter)

參數

textWriter
TextWriter

向其中寫入 TextWriterXDocument

範例

下列範例會 XDocument 建立 、將檔儲存至 StringWriter ,然後將字串列印至主控台。

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。 這個方法的行為是不會保留不重要的空白字元。

如果您想要控制空白字元,請使用 採用 做為參數的 Save SaveOptions 多載。 如需詳細資訊,請參閱 在載入或剖析 XML 時保留空白字元 ,以及在 序列化時保留空白字元

另請參閱

適用於

Save(String)

將此 XDocument 序列化至檔案,並覆寫現有的檔案 (如果存在)。

public:
 void Save(System::String ^ fileName);
public void Save (string fileName);
member this.Save : string -> unit
Public 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)

將這個 XDocument 輸出至指定的 Stream,選擇性地指定格式化行為。

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

這個 XDocument 輸出的目的資料流。

options
SaveOptions

指定格式化行為的 SaveOptions

備註

預設會將 options 設定為 None 。 此選項會移除所有無關的空白字元,並新增適當的無意義空白字元,以便正確縮排 XML。

如果您想要儲存未輸入的 XML,請指定 DisableFormattingoptions 旗標。 這會導致寫入器寫入所有空格,與 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 -> unit
Public 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,請指定 DisableFormattingoptions 旗標。 這會導致寫入器寫入所有空白字元,與 XML 樹狀結構中所表示完全相同。

如果您想要儲存縮排的 XML,請勿指定 DisableFormattingoptions 旗標。 這會移除所有無關的不重要空白字元,並新增適當的不重要空白字元,讓 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 -> unit
Public 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,請指定 DisableFormattingoptions 旗標。 這會導致寫入器寫入所有空白字元,與 XML 樹狀結構中所表示完全相同。

如果您想要儲存縮排的 XML,請勿指定 DisableFormattingoptions 旗標。 這會移除所有無關的不重要空白字元,並新增適當的不重要空白字元,讓 XML 正確縮排。 這是預設行為,以及不 options 做為參數之方法的多載 Save 行為。

如需詳細資訊,請參閱 在載入或剖析 XML 時保留空白字元 ,以及在 序列化時保留空白字元

另請參閱

適用於