XmlDocument.Save XmlDocument.Save XmlDocument.Save XmlDocument.Save Method

定義

將 XML 文件儲存至指定的位置。Saves the XML document to the specified location.

多載

Save(Stream) Save(Stream) Save(Stream) Save(Stream)

將 XML 文件儲存至指定的資料流。Saves the XML document to the specified stream.

Save(TextWriter) Save(TextWriter) Save(TextWriter) Save(TextWriter)

將 XML 文件儲存至指定的 TextWriterSaves the XML document to the specified TextWriter.

Save(String) Save(String) Save(String) Save(String)

將 XML 文件儲存至指定的檔案。Saves the XML document to the specified file. 如果指定的檔案存在,則這個方法會覆寫該檔案。If the specified file exists, this method overwrites it.

Save(XmlWriter) Save(XmlWriter) Save(XmlWriter) Save(XmlWriter)

將 XML 文件儲存至指定的 XmlWriterSaves the XML document to the specified XmlWriter.

Save(Stream) Save(Stream) Save(Stream) Save(Stream)

將 XML 文件儲存至指定的資料流。Saves the XML document to the specified stream.

public:
 virtual void Save(System::IO::Stream ^ outStream);
public virtual void Save (System.IO.Stream outStream);
abstract member Save : System.IO.Stream -> unit
override this.Save : System.IO.Stream -> unit
Public Overridable Sub Save (outStream As Stream)

參數

outStream
Stream Stream Stream Stream

要在其中儲存的資料流。The stream to which you want to save.

例外狀況

作業不會產生格式正確的 XML 文件 (例如,沒有文件項目或有重複的 XML 宣告)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

備註

只有當PreserveWhitespace設為true時, 才會保留空白字元。White space is preserved only if PreserveWhitespace is set to true.

目前XmlDocument物件的 XmlDeclaration 會決定已儲存檔中的編碼屬性。The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. Encoding 屬性的值是從XmlDeclaration.Encoding屬性取得。The value of the encoding attribute is taken from the XmlDeclaration.Encoding property. XmlDocument如果沒有 XmlDeclaration, 或如果 XmlDeclaration 沒有 encoding 屬性, 儲存的檔就不會有任何一個。If the XmlDocument does not have an XmlDeclaration, or if the XmlDeclaration does not have an encoding attribute, the saved document will not have one either.

儲存檔時, 會產生 xmlns 屬性來正確保存節點身分識別 (區功能變數名稱稱 + 命名空間 URI)。When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. 例如, 下列C#程式碼For example, the following C# code

XmlDocument doc = new XmlDocument();  
doc.AppendChild(doc.CreateElement("item","urn:1"));  
doc.Save(Console.Out);  

產生這個 xml 屬性<item xmls="urn:1"/>generates this xmls attribute <item xmls="urn:1"/>.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

請注意, 只有Save方法會強制執行格式正確的 XML 檔。Note that only the Save method enforces a well-formed XML document. 所有其他Save多載只保證格式正確的片段。All other Save overloads only guarantee a well-formed fragment.

Save(TextWriter) Save(TextWriter) Save(TextWriter) Save(TextWriter)

將 XML 文件儲存至指定的 TextWriterSaves the XML document to the specified TextWriter.

public:
 virtual void Save(System::IO::TextWriter ^ writer);
public virtual void Save (System.IO.TextWriter writer);
abstract member Save : System.IO.TextWriter -> unit
override this.Save : System.IO.TextWriter -> unit
Public Overridable Sub Save (writer As TextWriter)

參數

writer
TextWriter TextWriter TextWriter TextWriter

要儲存的目標 TextWriterThe TextWriter to which you want to save.

例外狀況

作業不會產生格式正確的 XML 文件 (例如,沒有文件項目或有重複的 XML 宣告)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

備註

TextWriter的編碼會決定寫出的編碼 (XmlDeclaration 節點的編碼方式會由的編碼TextWriter方式取代)。The encoding on the TextWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is replaced by the encoding of the TextWriter). 如果未在上TextWriter指定編碼XmlDocument , 則會儲存而不使用編碼屬性。If there was no encoding specified on the TextWriter, the XmlDocument is saved without an encoding attribute.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

請注意, 只有Save方法會強制執行格式正確的 XML 檔。Note that only the Save method enforces a well-formed XML document. 所有其他Save多載只保證格式正確的片段。All other Save overloads only guarantee a well-formed fragment.

Save(String) Save(String) Save(String) Save(String)

將 XML 文件儲存至指定的檔案。Saves the XML document to the specified file. 如果指定的檔案存在,則這個方法會覆寫該檔案。If the specified file exists, this method overwrites it.

public:
 virtual void Save(System::String ^ filename);
public virtual void Save (string filename);
abstract member Save : string -> unit
override this.Save : string -> unit
Public Overridable Sub Save (filename As String)

參數

filename
String String String String

您要儲存文件的目標檔案位置。The location of the file where you want to save the document.

例外狀況

作業不會產生格式正確的 XML 文件 (例如,沒有文件項目或有重複的 XML 宣告)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

範例

下列範例會將 XML 載入至物件、修改它, 然後將它儲存至名為 data .xml 的檔案。The following example loads XML into an XmlDocument object, modifies it, and then saves it to a file named data.xml.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<item><name>wrench</name></item>" );
   
   // Add a price element.
   XmlElement^ newElem = doc->CreateElement( "price" );
   newElem->InnerText = "10.95";
   doc->DocumentElement->AppendChild( newElem );
   
   // Save the document to a file. White space is
   // preserved (no white space).
   doc->PreserveWhitespace = true;
   doc->Save( "data.xml" );
}

using System;
using System.Xml;

public class Sample {

  public static void Main() {
 
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");

    // Add a price element.
    XmlElement newElem = doc.CreateElement("price");
    newElem.InnerText = "10.95";
    doc.DocumentElement.AppendChild(newElem);

    // Save the document to a file. White space is
    // preserved (no white space).
    doc.PreserveWhitespace = true;
    doc.Save("data.xml");
 
  }
}
Imports System.Xml

public class Sample 

  public shared sub Main() 
 
    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<item><name>wrench</name></item>")

    ' Add a price element.
    Dim newElem as XmlElement = doc.CreateElement("price")
    newElem.InnerText = "10.95"
    doc.DocumentElement.AppendChild(newElem)

    ' Save the document to a file. White space is
    ' preserved (no white space).
    doc.PreserveWhitespace = true
    doc.Save("data.xml")
 
  end sub
end class

資料 .xml 檔案將包含下列 XML: <item><name>wrench</name><price>10.95</price></item>The data.xml file will contain the following XML: <item><name>wrench</name><price>10.95</price></item>.

備註

只有在設為PreserveWhitespace true時, 才會在輸出檔中保留空白字元。White space is preserved in the output file only if PreserveWhitespace is set to true.

目前XmlDocument物件的 XmlDeclaration 會決定已儲存檔中的編碼屬性。The XmlDeclaration of the current XmlDocument object determines the encoding attribute in the saved document. Encoding 屬性的值是從XmlDeclaration.Encoding屬性取得。The value of the encoding attribute is taken from the XmlDeclaration.Encoding property. XmlDocument如果沒有 XmlDeclaration, 或如果 XmlDeclaration 沒有 encoding 屬性, 儲存的檔就不會有任何一個。If the XmlDocument does not have an XmlDeclaration, or if the XmlDeclaration does not have an encoding attribute, the saved document will not have one either.

儲存檔時, 會產生 xmlns 屬性來正確保存節點身分識別 (區功能變數名稱稱 + 命名空間 URI)。When the document is saved, xmlns attributes are generated to persist the node identity (local name + namespace URI) correctly. 例如, 下列C#程式碼For example, the following C# code

XmlDocument doc = new XmlDocument();  
doc.AppendChild(doc.CreateElement("item","urn:1"));  
doc.Save(Console.Out);  

產生這個 xml 屬性<item xmls="urn:1"/>generates this xmls attribute <item xmls="urn:1"/>.

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

請注意, 只有Save方法會強制執行格式正確的 XML 檔。Note that only the Save method enforces a well-formed XML document. 所有其他Save多載只保證格式正確的片段。All other Save overloads only guarantee a well-formed fragment.

Save(XmlWriter) Save(XmlWriter) Save(XmlWriter) Save(XmlWriter)

將 XML 文件儲存至指定的 XmlWriterSaves the XML document to the specified XmlWriter.

public:
 virtual void Save(System::Xml::XmlWriter ^ w);
public virtual void Save (System.Xml.XmlWriter w);
abstract member Save : System.Xml.XmlWriter -> unit
override this.Save : System.Xml.XmlWriter -> unit
Public Overridable Sub Save (w As XmlWriter)

參數

w
XmlWriter XmlWriter XmlWriter XmlWriter

要儲存的目標 XmlWriterThe XmlWriter to which you want to save.

例外狀況

作業不會產生格式正確的 XML 文件 (例如,沒有文件項目或有重複的 XML 宣告)。The operation would not result in a well formed XML document (for example, no document element or duplicate XML declarations).

範例

下列範例會將 XML XmlDocument載入至物件, 並將它儲存至檔案。The following example loads XML into an XmlDocument object and saves it out to a file.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<item><name>wrench</name></item>" );
   
   // Add a price element.
   XmlElement^ newElem = doc->CreateElement( "price" );
   newElem->InnerText = "10.95";
   doc->DocumentElement->AppendChild( newElem );
   
   // Save the document to a file and auto-indent the output.
   XmlTextWriter^ writer = gcnew XmlTextWriter( "data.xml", nullptr );
   writer->Formatting = Formatting::Indented;
   doc->Save( writer );
}

using System;
using System.Xml;

public class Sample {

  public static void Main() {
 
    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");

   // Add a price element.
   XmlElement newElem = doc.CreateElement("price");
   newElem.InnerText = "10.95";
   doc.DocumentElement.AppendChild(newElem);

   XmlWriterSettings settings = new XmlWriterSettings();
   settings.Indent = true;
   // Save the document to a file and auto-indent the output.
   XmlWriter writer = XmlWriter.Create("data.xml", settings);
    doc.Save(writer);
  }
}
Imports System.Xml

public class Sample 

  public shared sub Main() 
 
    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    doc.LoadXml("<item><name>wrench</name></item>")

   ' Add a price element.
   Dim newElem as XmlElement = doc.CreateElement("price")
   newElem.InnerText = "10.95"
   doc.DocumentElement.AppendChild(newElem)

   Dim settings As New XmlWriterSettings()
   settings.Indent = True
   ' Save the document to a file and auto-indent the output.
   Dim writer As XmlWriter = XmlWriter.Create("data.xml", settings)
    doc.Save(writer)
  end sub
end class

備註

只有當PreserveWhitespace設為true時, 才會保留空白字元。White space is preserved only if PreserveWhitespace is set to true.

XmlWriter的編碼會決定寫出的編碼 (XmlDeclaration 節點的編碼方式會由的編碼XmlWriter方式取代)。The encoding on the XmlWriter determines the encoding that is written out (The encoding of the XmlDeclaration node is replaced by the encoding of the XmlWriter). 如果未在上XmlWriter指定編碼XmlDocument , 則會儲存而不使用編碼屬性。If there was no encoding specified on the XmlWriter, the XmlDocument is saved without an encoding attribute.

儲存檔時, 會產生 xmlns 屬性來正確保存節點身分識別 (LocalName + NamespaceURI)。When the document is saved, xmlns attributes are generated to persist the node identity (LocalName + NamespaceURI) correctly. 例如, 下列C#程式碼For example, the following C# code

XmlDocument doc = new XmlDocument();  
doc.AppendChild(doc.CreateElement("item","urn:1"));  
doc.Save(Console.Out);  

產生此 xml 屬性:generates this xmls attribute:

<item  
    xmls="urn:1"/>  

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM).

請注意, 只有Save方法會強制執行格式正確的 XML 檔。Note that only the Save method enforces a well-formed XML document. 所有其他Save多載只保證格式正確的片段。All other Save overloads only guarantee a well-formed fragment.

另請參閱

適用於